dsh-mo-image 0.5.0 → 0.6.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.
@@ -18,6 +18,7 @@ export const SERVER_ERROR_CODES = [
18
18
  'provider.missingKey',
19
19
  'provider.missingKeyWithRef',
20
20
  'models.unsupported',
21
+ 'models.notFound',
21
22
  'adapter.unknownProtocol',
22
23
  'adapter.notFound',
23
24
  'config.credentialsUnavailable',
@@ -230,6 +230,79 @@ 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
+ /** 尺寸这类参数旧配置里可能存成了 STRING,读的时候按名字纠正一次。 */
241
+ const NUMERIC_ROLES = new Set(['width', 'height', 'seed', 'steps', 'batch_size']);
242
+
243
+ function paramType(param) {
244
+ const type = String(param.type || 'STRING');
245
+ if (NUMERIC_ROLES.has(param.name) && (type === 'STRING' || type === 'string')) return 'INT';
246
+ return type;
247
+ }
248
+
249
+ export function settingsPayload(state) {
250
+ const provider = (state && state.provider) || null;
251
+ const model = String((state && state.model) || '').trim();
252
+ const optimized = !(state && state.promptOptimization === false);
253
+ const payload = {
254
+ provider: provider ? String(provider.id || '') : '',
255
+ model: model,
256
+ promptOptimization: optimized,
257
+ hint: ''
258
+ };
259
+ if (!model) {
260
+ payload.hint = '还没有选择生图模型,请让用户先到设置里选一个';
261
+ return payload;
262
+ }
263
+ if (model === 'auto') {
264
+ payload.hint = optimized
265
+ ? '按你选中的那个模型的方言写' + NEED_FULL_RULES
266
+ : '直接用用户原话,不要扩写';
267
+ payload.models = (Array.isArray(state && state.models) ? state.models : []).filter(Boolean).map(String);
268
+ return payload;
269
+ }
270
+ const profile = resolvePromptProfile(model, (state && state.adapter) || null);
271
+ payload.hint = optimized
272
+ ? (PROFILE_HINTS[profile.id] || profile.label) + NEED_FULL_RULES
273
+ : '直接用用户原话,不要扩写,不要补 negative prompt';
274
+ const workflowParams = Array.isArray(state && state.workflowParams)
275
+ ? state.workflowParams.filter((param) => param && param.expose !== false)
276
+ : [];
277
+ if (workflowParams.length) {
278
+ // 参数只走结构化的 params,hint 里不再重复列一遍名字。
279
+ payload.params = workflowParams.map((param) => {
280
+ const out = { name: param.name, type: paramType(param) };
281
+ if (Array.isArray(param.options)) out.options = param.options;
282
+ return out;
283
+ });
284
+ payload.hint += ';可用 params 覆盖工作流参数,不传则用工作流默认值';
285
+ }
286
+ return payload;
287
+ }
288
+
289
+ /** 优化开启时补一句:完整写法只在没读过时才要加载,读完不用重复加载。 */
290
+ const NEED_FULL_RULES = ';完整写法没读过时先加载对应 skill,读过就不用再加载';
291
+
292
+ /** 每个模型族一句最短提示:就是 mo_image_settings 里 hint 的内容,别的都不要。 */
293
+ const PROFILE_HINTS = {
294
+ 'gpt-image': '自然语言:场景→主体→细节→约束;英文优先;无 negative prompt',
295
+ 'dall-e-3': '完整自然语言段落;英文优先;无 negative prompt',
296
+ 'nano-banana': '自然语言叙事,正向描述;中英皆可',
297
+ seedream: '简洁中文叙事,主体+行为+环境;中文优先',
298
+ 'qwen-image': '中文自然语言,可用 negative prompt;中文优先',
299
+ flux: '自然语言散文,主体+动作前置,30–80 词;英文优先;无 negative prompt',
300
+ 'stable-diffusion': '自然语言段落或短语标签;英文优先;可用 negative prompt',
301
+ wanx: '中文自然语言,简洁具体;可用 negative prompt',
302
+ kolors: '中文自然语言;可用 negative prompt',
303
+ generic: '自然语言写清主体、动作、环境、风格、光影、构图;中英皆可'
304
+ };
305
+
233
306
  /**
234
307
  * 生成注入系统提示词的那一节。没有当前模型时返回空串,整节省略。
235
308
  * @param {{model?: string, adapter?: {id?: string}|null, editModel?: string, editAdapter?: {id?: string}|null, promptOptimization?: boolean}} state
@@ -239,7 +312,7 @@ const DELIVERY_RULES = [
239
312
  '- 交付即止:工具返回图片链接后,最终回复只输出那一行;不评价画质、不描述画面。除非用户明确要求看图,否则不要打开或读取生成的图片。',
240
313
  '- 提供商由用户在设置里固定:不要自行切换提供商,也不要用其他工具(本地生图、建模/Blender、脚本绘图)替代生图模型。',
241
314
  '- 调用失败时:把错误原样告诉用户;可以问用户是否要更换模型或服务商,但不要自己更换,也不要换别的模型重试。',
242
- '- 想知道有哪些生图模型、当前选的是哪个时,优先调用 mo_image_list_models;配置文件可以只读查看,但对它的任何写入都会被守卫拒绝。'
315
+ '- 每次生图/改图前先调用 mo_image_settings 读当前配置,按它返回的模型写法和提示词优化状态写提示词;没读就调用生图工具会被拒绝。配置文件可以只读查看,但对它的任何写入都会被守卫拒绝。'
243
316
  ];
244
317
 
245
318
  /** Auto 行:用户把选择器设为 Auto 时,模型选择权才下放给对话模型。 */
@@ -268,7 +341,7 @@ export function promptSectionText(state) {
268
341
  '<mo-image-prompt>',
269
342
  '仅在用户需要生成/编辑图片时使用;其他任务忽略本节。',
270
343
  '当前生图模型:' + model + '。提示词优化:Off。',
271
- '- 调用生图工具时必须传 model:' + model + '(工具会核对;不传或传错都会被拒绝)。',
344
+ '- 不用传 model:模型由用户在选择器里决定(当前 ' + model + ');传了不一致的值会被拒绝。',
272
345
  '- 直接使用用户原话作为 prompt;不要扩写、改写、补充风格/光线/构图,不要替用户做提示词工程。',
273
346
  '- 只做最低限度的文本整理:保留用户原话中的主体、动作、文字、语言和限制;如果用户分多段描述,可以原样拼接,不添加用户没说的内容。',
274
347
  '- 不要自动补 negative_prompt;用户原话里没有的排除项不要加。',
@@ -290,7 +363,7 @@ export function promptSectionText(state) {
290
363
  lines.push('<mo-image-prompt>');
291
364
  lines.push('仅在用户需要生成/编辑图片时使用;其他任务忽略本节。');
292
365
  lines.push('当前生图模型:' + model + '(' + profile.label + ')。按该模型的提示词方言写。');
293
- lines.push('- 调用生图工具时必须传 model:' + model + '(工具会核对;不传或传错都会被拒绝)。');
366
+ lines.push('- 不用传 model:模型由用户在选择器里决定(当前 ' + model + ');传了不一致的值会被拒绝。');
294
367
  for (const rule of DELIVERY_RULES) lines.push(rule);
295
368
  lines.push('生成公式:' + GENERATION_FORMULA + '。');
296
369
  for (const rule of profile.rules) lines.push('- ' + rule);
@@ -305,7 +378,7 @@ export function promptSectionText(state) {
305
378
  } else {
306
379
  lines.push('编辑:' + profile.editRule);
307
380
  }
308
- lines.push('完整模板与示例:按需加载 skill mo-image-generation / mo-image-editing。');
381
+ lines.push('完整写法:没读过时按需加载 skill mo-image-generation / mo-image-editing;读过就不用重复加载。');
309
382
  lines.push('</mo-image-prompt>');
310
383
  return lines.join('\n');
311
384
  }
@@ -1,7 +1,10 @@
1
1
  /**
2
- * 更新状态服务:把「检查 GitHub → 有没有新版 → 装哪个 tag → 装完没有」
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:带 tag 的 git 依赖,和用户手动装的那条命令一致。 */
18
- export const UPDATE_SPEC = (version) => `github:CM-Silence/dsh-mo-image#v${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
- await active.removeBundle('dsh-mo-image');
206
- result = await active.installBundle(spec, installOptions);
207
- } else {
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
- phase = 'failed';
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
- phase = 'failed';
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