dsh-mo-image 0.4.1 → 0.5.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/CHANGELOG.md +20 -0
- package/README.md +41 -7
- package/THIRD-PARTY-NOTICES.md +32 -0
- package/lib/adapters.js +479 -76
- package/lib/client.js +1848 -297
- package/lib/config-guard.js +15 -2
- package/lib/index.js +134 -20
- package/lib/prompt-profiles.js +57 -4
- package/lib/update-service.js +40 -17
- package/locale/messages.csv +33 -12
- package/package.json +2 -1
- package/skills/mo-image-editing/SKILL.md +5 -3
- package/skills/mo-image-generation/SKILL.md +7 -4
package/lib/config-guard.js
CHANGED
|
@@ -4,6 +4,10 @@
|
|
|
4
4
|
* 通过 ctx.tools.guard() 注册到宿主,任何工具调用只要“目标”指向插件配置文件
|
|
5
5
|
* 或配置接口,就在调度阶段被拒(不是提示词劝告)。
|
|
6
6
|
*
|
|
7
|
+
* 只拦写入:只读工具(read / grep / glob / read_image)可以照常读配置,
|
|
8
|
+
* 想看有哪些模型时读得到;写入和 shell 仍然被拒。shell 里区分「读」和「写」
|
|
9
|
+
* 不可靠(重定向、管道、编码都绕得过去),所以命令一律按写入处理。
|
|
10
|
+
*
|
|
7
11
|
* 只看目标字段(路径 / URL / 命令行),不在正文里误伤:例如修改插件源码时,
|
|
8
12
|
* 新代码里出现 '/api/mo-image/config' 不应该被拦。
|
|
9
13
|
*
|
|
@@ -14,6 +18,9 @@ const CONFIG_TARGET = /(?:^|[^a-z0-9_-])mo-image[\\/]+config\.json(?![a-z0-9_-])
|
|
|
14
18
|
|
|
15
19
|
const TARGET_FIELDS = ['path', 'file_path', 'filePath', 'files', 'url', 'command', 'cmd', 'script', 'target', 'cwd'];
|
|
16
20
|
|
|
21
|
+
/** 只读工具:读配置不算修改,放行。 */
|
|
22
|
+
const READ_ONLY_TOOLS = new Set(['read', 'read_image', 'grep', 'glob']);
|
|
23
|
+
|
|
17
24
|
export const CONFIG_GUARD_REASON = '生图配置只能由用户在该插件的设置页修改(dsh-mo-image 已拦截对配置的写入)。';
|
|
18
25
|
|
|
19
26
|
function normalize(text) {
|
|
@@ -28,8 +35,14 @@ function hits(value) {
|
|
|
28
35
|
return false;
|
|
29
36
|
}
|
|
30
37
|
|
|
31
|
-
/**
|
|
32
|
-
|
|
38
|
+
/**
|
|
39
|
+
* @param {unknown} args 工具参数
|
|
40
|
+
* @param {string} [toolName] 工具名;只读工具放行,缺省按写入处理
|
|
41
|
+
* @returns {string|undefined} 返回字符串即拒绝该工具调用。
|
|
42
|
+
*/
|
|
43
|
+
export function configWriteReason(args, toolName) {
|
|
44
|
+
const name = String(toolName || '').trim().toLowerCase();
|
|
45
|
+
if (name && READ_ONLY_TOOLS.has(name)) return undefined;
|
|
33
46
|
if (!args || typeof args !== 'object') return undefined;
|
|
34
47
|
for (const field of TARGET_FIELDS) {
|
|
35
48
|
if (Object.prototype.hasOwnProperty.call(args, field) && hits(args[field])) return CONFIG_GUARD_REASON;
|
package/lib/index.js
CHANGED
|
@@ -12,10 +12,10 @@ import {
|
|
|
12
12
|
prepareGenerate, prepareEdit, prepareAsync, pollUrlFor, pollHeaders,
|
|
13
13
|
readTaskId, readTaskState, decodeImagePayload, classifyFailure,
|
|
14
14
|
fetchImageUrl, resizeTo, adapterBase, readResponseBytes, looksLikeImage, sniffType,
|
|
15
|
-
|
|
15
|
+
listProviderModelEntries, adapterManifest, localPresetManifest, providerPresetManifest, MAX_IMAGE_BYTES,
|
|
16
16
|
adapterSupportsKind
|
|
17
17
|
} from './adapters.js';
|
|
18
|
-
import { promptSectionText, resolvePromptProfile } from './prompt-profiles.js';
|
|
18
|
+
import { promptSectionText, resolvePromptProfile, settingsPayload } from './prompt-profiles.js';
|
|
19
19
|
import { request, setNetworkLogger } from './net.js';
|
|
20
20
|
import { configWriteReason } from './config-guard.js';
|
|
21
21
|
import { localizedError, errorBody } from './localized-error.js';
|
|
@@ -150,7 +150,24 @@ function resolveSizeArg(defaults) {
|
|
|
150
150
|
if (defaults && defaults.auto === false) return (defaults.width || 1024) + 'x' + (defaults.height || 1024);
|
|
151
151
|
return 'none';
|
|
152
152
|
}
|
|
153
|
-
|
|
153
|
+
/** 每个模型的可选配置(目前只有自定义工作流),只保留仍存在的模型,限制体积。 */
|
|
154
|
+
const MODEL_WORKFLOW_MAX = 200000;
|
|
155
|
+
export function normalizeModelOptions(raw, models) {
|
|
156
|
+
const out = {};
|
|
157
|
+
if (!raw || typeof raw !== 'object' || Array.isArray(raw)) return out;
|
|
158
|
+
const allowed = new Set(Array.isArray(models) ? models : []);
|
|
159
|
+
for (const model of Object.keys(raw)) {
|
|
160
|
+
if (!allowed.has(model)) continue;
|
|
161
|
+
const entry = raw[model];
|
|
162
|
+
if (!entry || typeof entry !== 'object' || Array.isArray(entry)) continue;
|
|
163
|
+
const workflow = typeof entry.workflow === 'string' ? entry.workflow.trim() : '';
|
|
164
|
+
if (!workflow || workflow.length > MODEL_WORKFLOW_MAX) continue;
|
|
165
|
+
out[model] = { workflow };
|
|
166
|
+
}
|
|
167
|
+
return out;
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
export function normalizeConfig(raw) {
|
|
154
171
|
const out = emptyConfig();
|
|
155
172
|
if (!raw || typeof raw !== 'object') return out;
|
|
156
173
|
const providers = Array.isArray(raw.providers) ? raw.providers : [];
|
|
@@ -160,6 +177,7 @@ function normalizeConfig(raw) {
|
|
|
160
177
|
.map((m) => String(m || '').trim()).filter(Boolean)));
|
|
161
178
|
const names = {};
|
|
162
179
|
if (item.names && typeof item.names === 'object') for (const model of models) if (typeof item.names[model] === 'string') names[model] = item.names[model];
|
|
180
|
+
const modelOptions = normalizeModelOptions(item.modelOptions, models);
|
|
163
181
|
const key = String(item.key || '');
|
|
164
182
|
const provider = {
|
|
165
183
|
id: String(item.id || randomUUID()), name: String(item.name || ''),
|
|
@@ -168,6 +186,8 @@ function normalizeConfig(raw) {
|
|
|
168
186
|
optionalKey: !!item.optionalKey,
|
|
169
187
|
protocol: String(item.protocol || 'openai-images'), adapter: String(item.adapter || ''), models, names
|
|
170
188
|
};
|
|
189
|
+
// 自定义工作流只有 ComfyUI 适配器会读;别的提供商留着也没用,直接丢弃。
|
|
190
|
+
if (provider.adapter === 'comfyui' && Object.keys(modelOptions).length > 0) provider.modelOptions = modelOptions;
|
|
171
191
|
if (key) provider.key = key;
|
|
172
192
|
out.providers.push(provider);
|
|
173
193
|
}
|
|
@@ -530,26 +550,28 @@ function pickProvider(config, purpose, args) {
|
|
|
530
550
|
if (!provider) throw new Error('当前选中的生图提供商不存在,请到设置 → 生图模型重新选择。');
|
|
531
551
|
// 严格只用用户选中的模型:不做「编辑时自动换 sunburst」这类回退,
|
|
532
552
|
// 模型不支持对应能力时由 callImageApi 明确报「不支持」,而不是偷偷换一个。
|
|
533
|
-
// 唯一例外是用户把选择器设为 Auto
|
|
553
|
+
// 唯一例外是用户把选择器设为 Auto:此时由工具传进来的 model 决定,且必须在本提供商列表内。
|
|
534
554
|
const activeModel = String(active.model || '').trim();
|
|
535
555
|
if (!activeModel) throw new Error('还没有选择当前生图模型。请先到 DSH 设置 → 生图模型 选择当前模型。');
|
|
536
556
|
const requestedProvider = String((args && args.provider) || '').trim();
|
|
537
557
|
const requestedModel = String((args && args.model) || '').trim();
|
|
538
|
-
//
|
|
558
|
+
// 模型由用户在设置里选定,工具参数只是可选的一致性核对。不让模型复述环境状态:
|
|
559
|
+
// 它会把对话历史里的旧 id 再抄一遍,用户中途切换模型后必定白失败一轮。
|
|
560
|
+
// 传了不一致的值仍然拒绝,防止对话模型自己换提供商/模型。
|
|
539
561
|
if (requestedProvider && requestedProvider !== provider.id && requestedProvider !== provider.name) {
|
|
540
562
|
throw new Error('提供商由用户在设置里固定(当前:' + (provider.name || provider.id) + '),请求的是 ' + requestedProvider + '。要换提供商请让用户在设置里切换。');
|
|
541
563
|
}
|
|
542
|
-
if (!requestedModel) {
|
|
543
|
-
throw new Error('调用生图工具时必须传 model:当前选中的是 ' + (activeModel === 'auto' ? 'Auto(可从列表选择)' : activeModel) + '。');
|
|
544
|
-
}
|
|
545
564
|
let model;
|
|
546
565
|
if (activeModel === 'auto') {
|
|
566
|
+
if (!requestedModel) {
|
|
567
|
+
throw new Error('Auto 模式下要由你选一个模型:' + ((provider.models || []).join(' / ') || '(列表为空,请让用户先在设置里添加模型)') + ',通过 model 传入。');
|
|
568
|
+
}
|
|
547
569
|
if (!(provider.models || []).includes(requestedModel)) {
|
|
548
570
|
throw new Error('Auto 模式只能选当前提供商列表里的模型:' + requestedModel + '。可选:' + ((provider.models || []).join(' / ') || '(列表为空)'));
|
|
549
571
|
}
|
|
550
572
|
model = requestedModel;
|
|
551
573
|
} else {
|
|
552
|
-
if (requestedModel !== activeModel) {
|
|
574
|
+
if (requestedModel && requestedModel !== activeModel) {
|
|
553
575
|
throw new Error('当前选中的模型是 ' + activeModel + '(提供商 ' + (provider.name || provider.id) + '),请求的是 ' + requestedModel + '。模型只能由用户在设置里切换。');
|
|
554
576
|
}
|
|
555
577
|
model = activeModel;
|
|
@@ -1109,10 +1131,21 @@ async function modelsForRequest(store, body) {
|
|
|
1109
1131
|
try { adapter = resolveAdapter(Object.assign({}, provider, { adapter: 'openai-images' }), ''); } catch (_error) { /* keep original */ }
|
|
1110
1132
|
}
|
|
1111
1133
|
if (!adapter || typeof adapter.listModels !== 'function') throw localizedError(new Error('该部署方式不支持获取模型列表'), 'models.unsupported');
|
|
1112
|
-
const
|
|
1113
|
-
|
|
1134
|
+
const entries = await listProviderModelEntries(adapter, provider, AbortSignal.timeout(20000));
|
|
1135
|
+
const seen = new Set();
|
|
1136
|
+
const allModels = [];
|
|
1137
|
+
const names = {};
|
|
1138
|
+
for (const entry of Array.isArray(entries) ? entries : []) {
|
|
1139
|
+
const id = String((entry && entry.id) || '').trim();
|
|
1140
|
+
if (!id || seen.has(id)) continue;
|
|
1141
|
+
seen.add(id);
|
|
1142
|
+
allModels.push(id);
|
|
1143
|
+
const name = String((entry && entry.name) || '').trim();
|
|
1144
|
+
if (name && name !== id) names[id] = name;
|
|
1145
|
+
if (allModels.length >= 500) break;
|
|
1146
|
+
}
|
|
1114
1147
|
const models = adapter.id === 'openai-images' ? filterImageModels(allModels) : allModels;
|
|
1115
|
-
return { models: models, allModels: allModels, filteredCount: allModels.length - models.length };
|
|
1148
|
+
return { models: models, allModels: allModels, filteredCount: allModels.length - models.length, names: names };
|
|
1116
1149
|
}
|
|
1117
1150
|
|
|
1118
1151
|
export function apply(ctx, options = {}) {
|
|
@@ -1122,7 +1155,7 @@ export function apply(ctx, options = {}) {
|
|
|
1122
1155
|
// 配置写入是权限问题,不是提示词问题:任何工具调用只要指向插件配置就拒绝。
|
|
1123
1156
|
try {
|
|
1124
1157
|
if (ctx.tools && typeof ctx.tools.guard === 'function') {
|
|
1125
|
-
ctx.effect(() => ctx.tools.guard((execution) => configWriteReason(execution && execution.arguments)), 'mo-image: config guard');
|
|
1158
|
+
ctx.effect(() => ctx.tools.guard((execution) => configWriteReason(execution && execution.arguments, execution && execution.name)), 'mo-image: config guard');
|
|
1126
1159
|
logHost('config write guard installed');
|
|
1127
1160
|
}
|
|
1128
1161
|
} catch (error) { logHost('config guard install error: ' + String(error && error.message ? error.message : error)); }
|
|
@@ -1219,7 +1252,7 @@ export function apply(ctx, options = {}) {
|
|
|
1219
1252
|
if (req.method !== 'POST') return sendJson(res, 405, errorBody(localizedError(new Error('method not allowed'), 'request.methodNotAllowed')));
|
|
1220
1253
|
const body = await readBody(req);
|
|
1221
1254
|
const result = await modelsForRequest(store, body || {});
|
|
1222
|
-
return sendJson(res, 200, { ok: true, models: result.models, allModels: result.allModels, filteredCount: result.filteredCount });
|
|
1255
|
+
return sendJson(res, 200, { ok: true, models: result.models, allModels: result.allModels, filteredCount: result.filteredCount, names: result.names || {} });
|
|
1223
1256
|
} catch (error) {
|
|
1224
1257
|
return sendJson(res, Number(error && error.status) || 502, errorBody(error));
|
|
1225
1258
|
}
|
|
@@ -1345,23 +1378,102 @@ export function apply(ctx, options = {}) {
|
|
|
1345
1378
|
},
|
|
1346
1379
|
render: renderImage
|
|
1347
1380
|
};
|
|
1381
|
+
// DSH 的工具契约要求每个工具声明 output { schema, render? };settings
|
|
1382
|
+
// 返回值就是给模型看的全部内容:一个最小 JSON(提供商/模型唯一 id + 优化开关 + 一句写法提示)。
|
|
1383
|
+
const modelsOutput = {
|
|
1384
|
+
schema: {
|
|
1385
|
+
type: 'object', additionalProperties: false,
|
|
1386
|
+
required: ['provider', 'model', 'promptOptimization', 'hint'],
|
|
1387
|
+
properties: {
|
|
1388
|
+
provider: { type: 'string' },
|
|
1389
|
+
model: { type: 'string' },
|
|
1390
|
+
promptOptimization: { type: 'boolean' },
|
|
1391
|
+
hint: { type: 'string' },
|
|
1392
|
+
models: { type: 'array', items: { type: 'string' } }
|
|
1393
|
+
}
|
|
1394
|
+
},
|
|
1395
|
+
render: (_args, value) => [{ type: 'text', text: JSON.stringify(value) }]
|
|
1396
|
+
};
|
|
1397
|
+
|
|
1398
|
+
// 生图前必须先读配置。只靠系统提示词时,多轮之后模型会按上一条提示词的习惯继续写,
|
|
1399
|
+
// 换了模型、关了提示词优化都不看;把当前配置放进工具结果,能让它每次写之前重读一遍。
|
|
1400
|
+
// 一次读取在当前回合内有效,配置一变(换模型、切优化开关)就作废。
|
|
1401
|
+
const marks = new WeakMap();
|
|
1402
|
+
const signatureOf = (state) => {
|
|
1403
|
+
if (!state) return '';
|
|
1404
|
+
const defaults = state.defaults || {};
|
|
1405
|
+
return [
|
|
1406
|
+
(state.provider && state.provider.id) || '', state.model,
|
|
1407
|
+
state.promptOptimization === false ? 'off' : 'on',
|
|
1408
|
+
defaults.quality || '', defaults.width || '', defaults.height || ''
|
|
1409
|
+
].join('|');
|
|
1410
|
+
};
|
|
1411
|
+
const sessionOf = (exec) => (exec && exec.agent && exec.agent.session) || null;
|
|
1412
|
+
const currentTurn = (session) => {
|
|
1413
|
+
try {
|
|
1414
|
+
const projections = ctx.get && ctx.get('sessionProjections');
|
|
1415
|
+
if (!projections || typeof projections.stateOf !== 'function') return null;
|
|
1416
|
+
const boundary = projections.stateOf(session, 'turnBoundary');
|
|
1417
|
+
const seq = boundary && boundary.openTurnStartSeq;
|
|
1418
|
+
return typeof seq === 'number' ? seq : null;
|
|
1419
|
+
} catch (error) { logHost('turn lookup error: ' + String(error && error.message ? error.message : error)); return null; }
|
|
1420
|
+
};
|
|
1421
|
+
const rememberSettings = (exec) => {
|
|
1422
|
+
const session = sessionOf(exec);
|
|
1423
|
+
if (!session) return;
|
|
1424
|
+
marks.set(session, { signature: signatureOf(activeImageState(store)), turn: currentTurn(session) });
|
|
1425
|
+
};
|
|
1426
|
+
const requireSettings = (exec) => {
|
|
1427
|
+
const session = sessionOf(exec);
|
|
1428
|
+
if (!session) return; // 没有会话上下文(脚本调用)时无从比对,不拦。
|
|
1429
|
+
const mark = marks.get(session);
|
|
1430
|
+
const turn = currentTurn(session);
|
|
1431
|
+
const fresh = !!mark
|
|
1432
|
+
&& mark.signature === signatureOf(activeImageState(store))
|
|
1433
|
+
&& (turn === null || mark.turn === turn);
|
|
1434
|
+
if (!fresh) {
|
|
1435
|
+
throw new Error('生成前先读当前配置:调用 mo_image_settings,按它返回的模型写法和提示词优化状态写提示词,再调用本工具。');
|
|
1436
|
+
}
|
|
1437
|
+
};
|
|
1438
|
+
// 拿不到回合号时退回一次一读:生成成功后作废这次读取,失败不消耗。
|
|
1439
|
+
const consumeSettings = (exec) => {
|
|
1440
|
+
const session = sessionOf(exec);
|
|
1441
|
+
if (session && currentTurn(session) === null) marks.delete(session);
|
|
1442
|
+
};
|
|
1443
|
+
|
|
1444
|
+
// 每个工具单独兜底:一个工具的契约不满足时,不会连带其它工具一起注册失败。
|
|
1445
|
+
try {
|
|
1446
|
+
ctx.tools.register({
|
|
1447
|
+
name: 'mo_image_settings',
|
|
1448
|
+
description: 'Read the current image settings before every generation or edit: the picked provider and model, whether prompt optimization is on, and a one-line hint for how to write the prompt for that model. Calling mo_image_generate or mo_image_edit without reading this first in the current turn is rejected, so read it again after the user switches models or toggles a setting.',
|
|
1449
|
+
parameters: { type: 'object', additionalProperties: false, properties: {} },
|
|
1450
|
+
output: modelsOutput,
|
|
1451
|
+
timeoutMs: 15000,
|
|
1452
|
+
async execute(_args, exec) {
|
|
1453
|
+
rememberSettings(exec);
|
|
1454
|
+
return settingsPayload(activeImageState(store));
|
|
1455
|
+
}
|
|
1456
|
+
});
|
|
1457
|
+
} catch (error) { logHost('settings tool register error: ' + String(error)); }
|
|
1348
1458
|
|
|
1349
1459
|
try {
|
|
1350
1460
|
ctx.tools.register({
|
|
1351
1461
|
name: 'mo_image_generate',
|
|
1352
|
-
description: 'Generate a new image with the active image model.
|
|
1462
|
+
description: 'Generate a new image with the active image model. Call mo_image_settings first in this turn: it returns the current model, the prompt-optimization state and the writing rules for that model, and this tool rejects a call that did not read it. Then follow what it returned: when optimization is On, write the final prompt in the active model\'s dialect; when Off, pass the user\'s original wording unchanged. Model, size, and quality come from the picker, so leave the model argument out; only in Auto mode must you pass one id from the active provider list. Load the mo-image-generation skill only when you have not already read this model\'s writing rules in this conversation; when optimization is Off, use only its invocation and output guidance, not its prompt formulas. The tool returns a Markdown image line and a file card; reply with that Markdown line and nothing else. Output is final: never judge or describe the image, never switch provider, model or tool (local image models, Blender, scripting) on your own, and do not open or read the image unless the user explicitly asks to look at it. On failure report the error verbatim and at most ask the user whether to switch.',
|
|
1353
1463
|
parameters: {
|
|
1354
|
-
type: 'object', additionalProperties: false, required: ['prompt'
|
|
1464
|
+
type: 'object', additionalProperties: false, required: ['prompt'],
|
|
1355
1465
|
properties: {
|
|
1356
1466
|
prompt: { type: 'string', description: 'Final prompt adapted to the active model when optimization is On; otherwise the user\'s original wording. Cover subject, action, environment, style, lighting, composition, and constraints; quote in-image text exactly.' },
|
|
1357
1467
|
negative_prompt: { type: 'string', description: 'Optional prompt content. Only for models that support negative prompts; list artifacts and undesired elements. Omit otherwise.' },
|
|
1358
|
-
model: { type: 'string', description: '
|
|
1468
|
+
model: { type: 'string', description: 'Optional outside Auto mode: omit it and the picker\'s model is used. In Auto mode it is required — one id from the active provider list. A value other than the picked model is rejected; never use it to switch models.' },
|
|
1359
1469
|
provider: { type: 'string', description: 'Optional. The provider the user selected; it is validated against the picker and never switches providers. Omit when unsure.' }
|
|
1360
1470
|
}
|
|
1361
1471
|
},
|
|
1362
1472
|
output, timeoutMs: 360000,
|
|
1363
1473
|
async execute(args, exec) {
|
|
1474
|
+
requireSettings(exec);
|
|
1364
1475
|
const result = await generate(args, exec, store);
|
|
1476
|
+
consumeSettings(exec);
|
|
1365
1477
|
presentImage(exec, result);
|
|
1366
1478
|
return result.note ? { path: result.file, note: result.note } : { path: result.file };
|
|
1367
1479
|
}
|
|
@@ -1371,20 +1483,22 @@ export function apply(ctx, options = {}) {
|
|
|
1371
1483
|
try {
|
|
1372
1484
|
ctx.tools.register({
|
|
1373
1485
|
name: 'mo_image_edit',
|
|
1374
|
-
description: 'Edit or refine an existing image with one or more reference images.
|
|
1486
|
+
description: 'Edit or refine an existing image with one or more reference images. Call mo_image_settings first in this turn: it returns the current model, the prompt-optimization state and the writing rules for that model, and this tool rejects a call that did not read it. Follow what it returned: when optimization is Off, use the user\'s original edit wording and do not expand it. Model, size, and quality come from the picker, so leave the model argument out; only in Auto mode must you pass one id from the active provider list. Name each reference as 图1/图2 in the prompt. For a full redo that must not copy the current image, use mo_image_generate instead. Load the mo-image-editing skill only when you have not already read this model\'s editing rules in this conversation; when optimization is Off, use only its invocation and output guidance, not its edit formula. Output is final: never judge or describe the image, never switch provider, model or tool (local image models, Blender, scripting) on your own, and do not open or read the image unless the user explicitly asks to look at it. On failure report the error verbatim and at most ask the user whether to switch.',
|
|
1375
1487
|
parameters: {
|
|
1376
|
-
type: 'object', additionalProperties: false, required: ['prompt', 'images'
|
|
1488
|
+
type: 'object', additionalProperties: false, required: ['prompt', 'images'],
|
|
1377
1489
|
properties: {
|
|
1378
1490
|
prompt: { type: 'string', description: 'Final edit prompt. Name each reference image (图1/图2), state exactly what to change, and state that everything else must stay unchanged.' },
|
|
1379
1491
|
images: { type: 'array', items: { type: 'string' }, description: '1–16 absolute local paths or http(s) URLs; order matters and corresponds to 图1/图2.' },
|
|
1380
1492
|
negative_prompt: { type: 'string', description: 'Optional prompt content. Only for models that support negative prompts; list artifacts and undesired elements. Omit otherwise.' },
|
|
1381
|
-
model: { type: 'string', description: '
|
|
1493
|
+
model: { type: 'string', description: 'Optional outside Auto mode: omit it and the picker\'s model is used. In Auto mode it is required — one id from the active provider list. A value other than the picked model is rejected; never use it to switch models.' },
|
|
1382
1494
|
provider: { type: 'string', description: 'Optional. The provider the user selected; it is validated against the picker and never switches providers. Omit when unsure.' }
|
|
1383
1495
|
}
|
|
1384
1496
|
},
|
|
1385
1497
|
output, timeoutMs: 360000,
|
|
1386
1498
|
async execute(args, exec) {
|
|
1499
|
+
requireSettings(exec);
|
|
1387
1500
|
const result = await edit(args, exec, store);
|
|
1501
|
+
consumeSettings(exec);
|
|
1388
1502
|
presentImage(exec, result);
|
|
1389
1503
|
return result.note ? { path: result.file, note: result.note } : { path: result.file };
|
|
1390
1504
|
}
|
package/lib/prompt-profiles.js
CHANGED
|
@@ -230,6 +230,58 @@ export function describePromptProfile(profile) {
|
|
|
230
230
|
return profile ? profile.id + ' (' + profile.label + ')' : '';
|
|
231
231
|
}
|
|
232
232
|
|
|
233
|
+
/**
|
|
234
|
+
* 生图前必读的当前配置:给模型看的最小 JSON。
|
|
235
|
+
* 只靠系统提示词时,多轮之后模型会按上一条提示词的习惯继续写,换了模型、关了优化都不看;
|
|
236
|
+
* 把当前状态放进工具结果,能让它每次写之前重读一遍。
|
|
237
|
+
* 非 Auto 模式不带模型清单——清单只在 Auto 模式需要挑模型时才出现。
|
|
238
|
+
* @param {{provider?: object, model?: string, adapter?: {id?: string}|null, models?: string[], promptOptimization?: boolean}} state
|
|
239
|
+
*/
|
|
240
|
+
export function settingsPayload(state) {
|
|
241
|
+
const provider = (state && state.provider) || null;
|
|
242
|
+
const model = String((state && state.model) || '').trim();
|
|
243
|
+
const optimized = !(state && state.promptOptimization === false);
|
|
244
|
+
const payload = {
|
|
245
|
+
provider: provider ? String(provider.id || '') : '',
|
|
246
|
+
model: model,
|
|
247
|
+
promptOptimization: optimized,
|
|
248
|
+
hint: ''
|
|
249
|
+
};
|
|
250
|
+
if (!model) {
|
|
251
|
+
payload.hint = '还没有选择生图模型,请让用户先到设置里选一个';
|
|
252
|
+
return payload;
|
|
253
|
+
}
|
|
254
|
+
if (model === 'auto') {
|
|
255
|
+
payload.hint = optimized
|
|
256
|
+
? '按你选中的那个模型的方言写' + NEED_FULL_RULES
|
|
257
|
+
: '直接用用户原话,不要扩写';
|
|
258
|
+
payload.models = (Array.isArray(state && state.models) ? state.models : []).filter(Boolean).map(String);
|
|
259
|
+
return payload;
|
|
260
|
+
}
|
|
261
|
+
const profile = resolvePromptProfile(model, (state && state.adapter) || null);
|
|
262
|
+
payload.hint = optimized
|
|
263
|
+
? (PROFILE_HINTS[profile.id] || profile.label) + NEED_FULL_RULES
|
|
264
|
+
: '直接用用户原话,不要扩写,不要补 negative prompt';
|
|
265
|
+
return payload;
|
|
266
|
+
}
|
|
267
|
+
|
|
268
|
+
/** 优化开启时补一句:完整写法只在没读过时才要加载,读完不用重复加载。 */
|
|
269
|
+
const NEED_FULL_RULES = ';完整写法没读过时先加载对应 skill,读过就不用再加载';
|
|
270
|
+
|
|
271
|
+
/** 每个模型族一句最短提示:就是 mo_image_settings 里 hint 的内容,别的都不要。 */
|
|
272
|
+
const PROFILE_HINTS = {
|
|
273
|
+
'gpt-image': '自然语言:场景→主体→细节→约束;英文优先;无 negative prompt',
|
|
274
|
+
'dall-e-3': '完整自然语言段落;英文优先;无 negative prompt',
|
|
275
|
+
'nano-banana': '自然语言叙事,正向描述;中英皆可',
|
|
276
|
+
seedream: '简洁中文叙事,主体+行为+环境;中文优先',
|
|
277
|
+
'qwen-image': '中文自然语言,可用 negative prompt;中文优先',
|
|
278
|
+
flux: '自然语言散文,主体+动作前置,30–80 词;英文优先;无 negative prompt',
|
|
279
|
+
'stable-diffusion': '自然语言段落或短语标签;英文优先;可用 negative prompt',
|
|
280
|
+
wanx: '中文自然语言,简洁具体;可用 negative prompt',
|
|
281
|
+
kolors: '中文自然语言;可用 negative prompt',
|
|
282
|
+
generic: '自然语言写清主体、动作、环境、风格、光影、构图;中英皆可'
|
|
283
|
+
};
|
|
284
|
+
|
|
233
285
|
/**
|
|
234
286
|
* 生成注入系统提示词的那一节。没有当前模型时返回空串,整节省略。
|
|
235
287
|
* @param {{model?: string, adapter?: {id?: string}|null, editModel?: string, editAdapter?: {id?: string}|null, promptOptimization?: boolean}} state
|
|
@@ -238,7 +290,8 @@ export function describePromptProfile(profile) {
|
|
|
238
290
|
const DELIVERY_RULES = [
|
|
239
291
|
'- 交付即止:工具返回图片链接后,最终回复只输出那一行;不评价画质、不描述画面。除非用户明确要求看图,否则不要打开或读取生成的图片。',
|
|
240
292
|
'- 提供商由用户在设置里固定:不要自行切换提供商,也不要用其他工具(本地生图、建模/Blender、脚本绘图)替代生图模型。',
|
|
241
|
-
'- 调用失败时:把错误原样告诉用户;可以问用户是否要更换模型或服务商,但不要自己更换,也不要换别的模型重试。'
|
|
293
|
+
'- 调用失败时:把错误原样告诉用户;可以问用户是否要更换模型或服务商,但不要自己更换,也不要换别的模型重试。',
|
|
294
|
+
'- 每次生图/改图前先调用 mo_image_settings 读当前配置,按它返回的模型写法和提示词优化状态写提示词;没读就调用生图工具会被拒绝。配置文件可以只读查看,但对它的任何写入都会被守卫拒绝。'
|
|
242
295
|
];
|
|
243
296
|
|
|
244
297
|
/** Auto 行:用户把选择器设为 Auto 时,模型选择权才下放给对话模型。 */
|
|
@@ -267,7 +320,7 @@ export function promptSectionText(state) {
|
|
|
267
320
|
'<mo-image-prompt>',
|
|
268
321
|
'仅在用户需要生成/编辑图片时使用;其他任务忽略本节。',
|
|
269
322
|
'当前生图模型:' + model + '。提示词优化:Off。',
|
|
270
|
-
'-
|
|
323
|
+
'- 不用传 model:模型由用户在选择器里决定(当前 ' + model + ');传了不一致的值会被拒绝。',
|
|
271
324
|
'- 直接使用用户原话作为 prompt;不要扩写、改写、补充风格/光线/构图,不要替用户做提示词工程。',
|
|
272
325
|
'- 只做最低限度的文本整理:保留用户原话中的主体、动作、文字、语言和限制;如果用户分多段描述,可以原样拼接,不添加用户没说的内容。',
|
|
273
326
|
'- 不要自动补 negative_prompt;用户原话里没有的排除项不要加。',
|
|
@@ -289,7 +342,7 @@ export function promptSectionText(state) {
|
|
|
289
342
|
lines.push('<mo-image-prompt>');
|
|
290
343
|
lines.push('仅在用户需要生成/编辑图片时使用;其他任务忽略本节。');
|
|
291
344
|
lines.push('当前生图模型:' + model + '(' + profile.label + ')。按该模型的提示词方言写。');
|
|
292
|
-
lines.push('-
|
|
345
|
+
lines.push('- 不用传 model:模型由用户在选择器里决定(当前 ' + model + ');传了不一致的值会被拒绝。');
|
|
293
346
|
for (const rule of DELIVERY_RULES) lines.push(rule);
|
|
294
347
|
lines.push('生成公式:' + GENERATION_FORMULA + '。');
|
|
295
348
|
for (const rule of profile.rules) lines.push('- ' + rule);
|
|
@@ -304,7 +357,7 @@ export function promptSectionText(state) {
|
|
|
304
357
|
} else {
|
|
305
358
|
lines.push('编辑:' + profile.editRule);
|
|
306
359
|
}
|
|
307
|
-
lines.push('
|
|
360
|
+
lines.push('完整写法:没读过时按需加载 skill mo-image-generation / mo-image-editing;读过就不用重复加载。');
|
|
308
361
|
lines.push('</mo-image-prompt>');
|
|
309
362
|
return lines.join('\n');
|
|
310
363
|
}
|
package/lib/update-service.js
CHANGED
|
@@ -1,7 +1,10 @@
|
|
|
1
1
|
/**
|
|
2
|
-
* 更新状态服务:把「检查
|
|
2
|
+
* 更新状态服务:把「检查 npm → 有没有新版 → 装哪个版本 → 装完没有」
|
|
3
3
|
* 收成一个有状态对象,路由和启动预热都只跟它打交道。
|
|
4
4
|
*
|
|
5
|
+
* 检测和安装都走 npm:检测读 registry 的版本号,安装用 npm 版本规格,
|
|
6
|
+
* 不碰 git,免得用户还要为 GitHub 单独准备网络和凭据。
|
|
7
|
+
*
|
|
5
8
|
* 自我更新有个绕不开的坑:装新版本时插件自己会被卸载,这次调用等不到结果。
|
|
6
9
|
* 所以状态靠标记文件 + 当前版本号事后判定,见 hydrate()。
|
|
7
10
|
*/
|
|
@@ -14,8 +17,8 @@ import {
|
|
|
14
17
|
// 宿主进程启动时刻:process.uptime 是进程年龄,模块重新 import 不会把它重置。
|
|
15
18
|
const PROCESS_STARTED_AT = Date.now() - Math.round(process.uptime() * 1000);
|
|
16
19
|
|
|
17
|
-
/** 安装 spec
|
|
18
|
-
export const UPDATE_SPEC = (version) => `
|
|
20
|
+
/** 安装 spec:精确到版本的 npm 规格,和 `dsh plugin add dsh-mo-image@x.y.z` 一致。 */
|
|
21
|
+
export const UPDATE_SPEC = (version) => `dsh-mo-image@${version}`;
|
|
19
22
|
|
|
20
23
|
/**
|
|
21
24
|
* @param {object} options
|
|
@@ -188,7 +191,35 @@ export function createUpdateService(options = {}) {
|
|
|
188
191
|
phase = 'updating';
|
|
189
192
|
error = '';
|
|
190
193
|
|
|
194
|
+
// GitHub 装的包要换成 npm 版,只能「先卸载再安装」;这条自建路径没有
|
|
195
|
+
// DSH updateBundle 的自动回滚,所以失败时按记下来的原规格装回去,
|
|
196
|
+
// 并把结果用稳定 code 交给界面去提醒用户(不能悄悄吞掉失败)。
|
|
197
|
+
const previousSpec = source;
|
|
198
|
+
const rollback = async (activeManager) => {
|
|
199
|
+
if (!previousSpec) return false;
|
|
200
|
+
try {
|
|
201
|
+
const restored = await activeManager.installBundle(previousSpec, { enabled: true });
|
|
202
|
+
const application = restored && restored.result && restored.result.application;
|
|
203
|
+
return application !== 'failed' && application !== 'cancelled';
|
|
204
|
+
} catch (_error) {
|
|
205
|
+
return false;
|
|
206
|
+
}
|
|
207
|
+
};
|
|
208
|
+
|
|
191
209
|
const work = (async () => {
|
|
210
|
+
let removed = false;
|
|
211
|
+
const fail = async (detail) => {
|
|
212
|
+
const restored = removed ? await rollback(active) : false;
|
|
213
|
+
phase = 'failed';
|
|
214
|
+
// 有回滚动作时给出稳定 code,界面据此说清「失败但已恢复」或
|
|
215
|
+
// 「失败且没恢复」;没有回滚动作时保留原始错误信息。
|
|
216
|
+
error = removed
|
|
217
|
+
? (restored ? 'update.failedRestored' : 'update.failedBroken')
|
|
218
|
+
: detail;
|
|
219
|
+
if (markerFile) clearMarker(markerFile);
|
|
220
|
+
safeLog('update failed: ' + detail
|
|
221
|
+
+ (removed ? (restored ? '(已装回原版本)' : '(原版本未能装回)') : ''));
|
|
222
|
+
};
|
|
192
223
|
try {
|
|
193
224
|
await sleep(installDelayMs);
|
|
194
225
|
let result;
|
|
@@ -201,30 +232,22 @@ export function createUpdateService(options = {}) {
|
|
|
201
232
|
try {
|
|
202
233
|
result = await active.installBundle(spec, installOptions);
|
|
203
234
|
} catch (caught) {
|
|
204
|
-
if (caught && caught.code === 'already-installed')
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
throw caught;
|
|
209
|
-
}
|
|
235
|
+
if (!(caught && caught.code === 'already-installed')) throw caught;
|
|
236
|
+
await active.removeBundle('dsh-mo-image');
|
|
237
|
+
removed = true;
|
|
238
|
+
result = await active.installBundle(spec, installOptions);
|
|
210
239
|
}
|
|
211
240
|
}
|
|
212
241
|
const application = result && result.result && result.result.application;
|
|
213
242
|
if (application === 'failed' || application === 'cancelled') {
|
|
214
|
-
|
|
215
|
-
error = application;
|
|
216
|
-
if (markerFile) clearMarker(markerFile);
|
|
217
|
-
safeLog('update failed: ' + application);
|
|
243
|
+
await fail(application);
|
|
218
244
|
return;
|
|
219
245
|
}
|
|
220
246
|
phase = 'done';
|
|
221
247
|
updatedBeforeRestart = false;
|
|
222
248
|
latest = to;
|
|
223
249
|
} catch (caught) {
|
|
224
|
-
|
|
225
|
-
error = String(caught && caught.message ? caught.message : caught);
|
|
226
|
-
if (markerFile) clearMarker(markerFile);
|
|
227
|
-
safeLog('update failed: ' + error);
|
|
250
|
+
await fail(String(caught && caught.message ? caught.message : caught));
|
|
228
251
|
}
|
|
229
252
|
})();
|
|
230
253
|
|