dsh-qqbot-muu 0.1.15 → 0.1.21

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.
Files changed (171) hide show
  1. package/CHANGELOG.md +168 -85
  2. package/client/qqbot-settings.js +13401 -12024
  3. package/config/modules.json +212 -0
  4. package/dist/channel-tools.js +21 -10
  5. package/dist/config.d.ts +14 -0
  6. package/dist/config.js +573 -449
  7. package/dist/features/contextless-store.js +247 -247
  8. package/dist/features/contextless-trim.js +455 -34
  9. package/dist/features/four-source.js +338 -336
  10. package/dist/features/group-join-request.js +289 -287
  11. package/dist/features/local-models.js +1494 -0
  12. package/dist/features/rotate-summarizer.js +438 -0
  13. package/dist/features/scenario-detect.js +321 -0
  14. package/dist/features/scheduler.js +8 -1
  15. package/dist/features/session-rotate.js +499 -0
  16. package/dist/features/session-title.js +199 -0
  17. package/dist/features/style-layer.js +501 -0
  18. package/dist/features/thinking-log.js +71 -69
  19. package/dist/gateway/bootstrap.js +1704 -1388
  20. package/dist/gateway/data-root.js +77 -3
  21. package/dist/gateway/middleware-setup.js +6 -3
  22. package/dist/index.js +4 -2
  23. package/dist/model/settings-reader.js +193 -102
  24. package/dist/session/session-manager.js +1730 -1587
  25. package/dist/shared/paths.js +128 -0
  26. package/dist/shared/settings-io.js +79 -0
  27. package/dist/transport/inbound.js +20 -1
  28. package/dist/transport/outbound.js +56 -4
  29. package/docs/RELEASE_CHECKLIST.md +24 -2
  30. package/dshqqbot/.qqbot-extensions/hooks/interview-panel.mjs +41 -1
  31. package/dshqqbot/.qqbot-extensions/inbound/00-vision.mjs +111 -0
  32. package/dshqqbot/.qqbot-extensions/inbound/aa-memory-recall.mjs +802 -689
  33. package/dshqqbot/.qqbot-extensions/inbound/ac-image-see.mjs +1 -1
  34. package/dshqqbot/.qqbot-extensions/inbound/az-feedback.mjs +113 -115
  35. package/dshqqbot/.qqbot-extensions/inbound/local-answer.mjs +689 -568
  36. package/dshqqbot/.qqbot-extensions/lib/.pet-form.json +1 -1
  37. package/dshqqbot/.qqbot-extensions/lib/draw-core.mjs +238 -7
  38. package/dshqqbot/.qqbot-extensions/lib/local-llm.mjs +455 -394
  39. package/dshqqbot/.qqbot-extensions/lib/memory-agent.mjs +930 -928
  40. package/dshqqbot/.qqbot-extensions/lib/memory-core.mjs +537 -537
  41. package/dshqqbot/.qqbot-extensions/lib/memory-db.mjs +2476 -2245
  42. package/dshqqbot/.qqbot-extensions/lib/memory-hippo.mjs +18 -0
  43. package/dshqqbot/.qqbot-extensions/lib/memory.mjs +191 -186
  44. package/dshqqbot/.qqbot-extensions/lib/plugin-dir.mjs +14 -4
  45. package/dshqqbot/.qqbot-extensions/lib/scope-util.mjs +84 -0
  46. package/dshqqbot/.qqbot-extensions/lib/vision.mjs +1 -1
  47. package/dshqqbot/.qqbot-extensions/outbound/aa-sanitize.mjs +158 -154
  48. package/dshqqbot/.qqbot-extensions/outbound/ac-media-fix.mjs +5 -1
  49. package/dshqqbot/.qqbot-extensions/outbound/memory-agent.mjs +121 -130
  50. package/dshqqbot/.qqbot-extensions/outbound/split-reply.mjs +18 -2
  51. package/dshqqbot/.qqbot-extensions/outbound/turn-memory.mjs +92 -101
  52. package/dshqqbot/.qqbot-extensions/tools/draw.mjs +22 -1
  53. package/dshqqbot/.qqbot-extensions/tools/memory.mjs +178 -120
  54. package/dshqqbot/.qqbot-extensions/tools/pet_card.mjs +3 -2
  55. package/dshqqbot/.qqbot-extensions/tools/storyboard.mjs +3 -1
  56. package/dshqqbot/.qqbot-extensions/vendor/abogus/test-abogus.mjs +128 -126
  57. package/package.json +119 -111
  58. package/scripts/__pycache__/fan-mode.cpython-311.pyc +0 -0
  59. package/scripts/__pycache__/memory-audit.cpython-311.pyc +0 -0
  60. package/scripts/__pycache__/qq-ip-sync.cpython-311.pyc +0 -0
  61. package/scripts/__pycache__/qq-whitelist.cpython-311.pyc +0 -0
  62. package/scripts/_add_maintenance_cfg.py +20 -0
  63. package/scripts/_bench_keeplease.sh +10 -0
  64. package/scripts/_dbg_purge.py +15 -0
  65. package/scripts/_elev.sh +6 -0
  66. package/scripts/_fix_all_admin.cmd +16 -0
  67. package/scripts/_livecheck-image.mjs +33 -0
  68. package/scripts/_livecheck-llm.mjs +34 -0
  69. package/scripts/_livecheck-tts.mjs +37 -0
  70. package/scripts/_patch_curve.py +88 -0
  71. package/scripts/_patch_fan_thermal.py +24 -0
  72. package/scripts/_tag_tmp.py +13 -0
  73. package/scripts/adb-broker-client.mjs +87 -0
  74. package/scripts/adb-broker.mjs +205 -0
  75. package/scripts/ai-mode.cmd +6 -0
  76. package/scripts/ai-mode.py +387 -0
  77. package/scripts/ai-toolkit.py +607 -0
  78. package/scripts/apply-fan-gpu45.py +51 -0
  79. package/scripts/bench-fans.py +515 -0
  80. package/scripts/bench-load-direct.py +63 -0
  81. package/scripts/boot-tasks-hidden.vbs +20 -0
  82. package/scripts/boot-tasks.cmd +277 -0
  83. package/scripts/chat-archive/__pycache__/build-sft-v4.cpython-311.pyc +0 -0
  84. package/scripts/chat-archive/__pycache__/build-sft.cpython-311.pyc +0 -0
  85. package/scripts/chat-archive/build-sft-v4.py +566 -0
  86. package/scripts/chat-archive/build-sft.py +390 -0
  87. package/scripts/chat-archive/eval-sft.py +155 -0
  88. package/scripts/chat-archive/extract-memory.py +143 -0
  89. package/scripts/chat-archive/inventory.py +182 -0
  90. package/scripts/chat-archive/merge-sft.py +114 -0
  91. package/scripts/chat-archive/qce-pull.py +217 -0
  92. package/scripts/chat-archive/qqheader.py +110 -0
  93. package/scripts/chat-archive/qqkey2.py +113 -0
  94. package/scripts/chat-archive/qqprobe.py +153 -0
  95. package/scripts/chat-archive/screen-collect.py +163 -0
  96. package/scripts/chat-archive/sqlcipher_probe.py +444 -0
  97. package/scripts/comfy_gen.py +209 -0
  98. package/scripts/compose-video.py +168 -0
  99. package/scripts/dl_models.py +45 -0
  100. package/scripts/draw-now.cmd +18 -0
  101. package/scripts/dsh-instance-guard.mjs +20 -2
  102. package/scripts/elev-extra.cmd +25 -0
  103. package/scripts/episode.py +529 -0
  104. package/scripts/fan-curve.py +218 -0
  105. package/scripts/fan-guard.cmd +8 -0
  106. package/scripts/fan-mode.py +380 -0
  107. package/scripts/fan_manual_safe.py +33 -0
  108. package/scripts/fan_restore_manual.py +46 -0
  109. package/scripts/fc_curves.py +359 -0
  110. package/scripts/fix-fan-and-timers.cmd +39 -0
  111. package/scripts/fix_admin_all.py +94 -0
  112. package/scripts/flows.mjs +224 -0
  113. package/scripts/gateway.cmd +126 -0
  114. package/scripts/gen-image.py +420 -0
  115. package/scripts/gen-video.py +186 -0
  116. package/scripts/git-askpass.cmd +2 -2
  117. package/scripts/gpu-mode.py +195 -0
  118. package/scripts/hw-profile.py +175 -0
  119. package/scripts/install-schedule.cmd +37 -0
  120. package/scripts/install-watch.cmd +29 -0
  121. package/scripts/matte-cutout.py +106 -0
  122. package/scripts/media-enhance.py +402 -0
  123. package/scripts/memory-audit.py +581 -0
  124. package/scripts/migrate-scope.mjs +140 -139
  125. package/scripts/model-registry.py +519 -0
  126. package/scripts/openid-audit.mjs +219 -0
  127. package/scripts/pull-gguf.py +127 -0
  128. package/scripts/purge-standby.py +151 -0
  129. package/scripts/qq-connect-login.py +133 -0
  130. package/scripts/qq-ip-sync-hidden.vbs +5 -0
  131. package/scripts/qq-ip-sync.py +300 -0
  132. package/scripts/qq-login-once.cmd +20 -0
  133. package/scripts/qq-login-qr.py +72 -0
  134. package/scripts/qq-whitelist-fix.cmd +27 -0
  135. package/scripts/qq-whitelist-login.cmd +26 -0
  136. package/scripts/qq-whitelist.cmd +12 -0
  137. package/scripts/qq-whitelist.py +1452 -0
  138. package/scripts/quality-compare.py +134 -0
  139. package/scripts/release-all.mjs +636 -430
  140. package/scripts/release-check.mjs +244 -226
  141. package/scripts/resume-coder.cmd +38 -0
  142. package/scripts/secure-room/index.html +334 -0
  143. package/scripts/secure-room/server.py +582 -0
  144. package/scripts/security-check.mjs +164 -0
  145. package/scripts/selftest-local-llm.mjs +20 -0
  146. package/scripts/selftest-local-models.mjs +754 -0
  147. package/scripts/selftest-rotate-style.mjs +308 -0
  148. package/scripts/shutdown-s5-optional.cmd +21 -0
  149. package/scripts/sleep-2325.cmd +75 -0
  150. package/scripts/start-comfyui.cmd +5 -0
  151. package/scripts/start-strata.cmd +17 -0
  152. package/scripts/start-strata.vbs +11 -0
  153. package/scripts/start-watch-hidden.vbs +3 -0
  154. package/scripts/stickers/auto-tag.mjs +161 -0
  155. package/scripts/storage.mjs +236 -0
  156. package/scripts/sync-plugin-copies.mjs +98 -23
  157. package/scripts/tests/test-qq-whitelist.py +192 -0
  158. package/scripts/train/__pycache__/train-lora.cpython-311.pyc +0 -0
  159. package/scripts/train/gguf-quantize.py +92 -0
  160. package/scripts/train/gguf-to-hf.py +112 -0
  161. package/scripts/train/merge-lora.py +51 -0
  162. package/scripts/train/probe-mirrors.sh +12 -0
  163. package/scripts/train/probe-mirrors2.sh +20 -0
  164. package/scripts/train/setup-wsl.sh +23 -0
  165. package/scripts/train/setup-wsl2.sh +25 -0
  166. package/scripts/train/setup-wsl3.sh +22 -0
  167. package/scripts/train/setup-wsl4.sh +22 -0
  168. package/scripts/train/train-lora.py +319 -0
  169. package/scripts/vram-free.py +104 -0
  170. package/scripts/wechat-full-pull.mjs +1 -1
  171. package/settings-host.js +1047 -12
@@ -1 +1 @@
1
- {"form":"muu","at":1794137836082}
1
+ {"form":"muu","at":1795965167704}
@@ -56,6 +56,28 @@ export const DEFAULTS = {
56
56
  // 默认用 SD1.5 动漫模型(单文件):sd-turbo 2 步出来的图糊得没法看
57
57
  modelDir: __repoRoot() + "/.models/counterfeit3/Counterfeit-V3.0_fix_fp16.safetensors",
58
58
  baseUrl: 'http://127.0.0.1:7861',
59
+ /**
60
+ * 出图后端:
61
+ * sdserver = 旧的 7861 python 微服务(SD1.5 Counterfeit,768px 6 秒,画质一般)
62
+ * comfyui = 直接打 ComfyUI 8191 的 FLUX.1-schnell GGUF(1024px 约 50 秒,画质强很多)
63
+ * 选 comfyui 后 baseUrl/sd* 全部不参与,走 comfyUrl。
64
+ */
65
+ backend: 'sdserver',
66
+ comfyUrl: 'http://127.0.0.1:8191',
67
+ comfyStartBat: __repoRoot() + "/services/comfyui/start_comfyui_8191.bat",
68
+ comfyUnet: 'flux1-schnell-Q6_K.gguf',
69
+ comfyClipL: 'clip_l.safetensors',
70
+ comfyT5: 't5-v1_1-xxl-encoder-Q5_K_M.gguf',
71
+ comfyVae: 'ae.safetensors',
72
+ comfySteps: 4,
73
+ /** 架构:flux(默认,UnetLoaderGGUF+DualCLIPLoader) | qwen(Qwen-Image-2512,CLIPLoader type=qwen_image) */
74
+ comfyArch: 'flux',
75
+ /** qwen 档的文本塔与 CFG/Shift(flux 档不用这几个键) */
76
+ comfyTe: '',
77
+ comfyCfg: 2.5,
78
+ comfyShift: 3.1,
79
+ /** FLUX dev / krea 的提示词遵循强度(schnell 保持 0,不加 FluxGuidance 节点) */
80
+ comfyGuidance: 0,
59
81
  /** 单张图最多等多久(首次含加载) */
60
82
  timeoutMs: 120000,
61
83
  /** 启动服务后等它把模型加载完的上限 */
@@ -126,6 +148,21 @@ export function listProfiles(cfg) {
126
148
  return Object.keys(p);
127
149
  }
128
150
 
151
+ /**
152
+ * 取某个档位的覆盖字段(2026-11-27「按需切换」):
153
+ * activeProfile 是**写死在配置里的默认档**;这个函数给单次调用用,
154
+ * 不改配置文件、不影响其它人,只把这一个请求切到指定档(例如认真画时才用 qwen)。
155
+ * 未知档位返回 null,调用方要回一份「有哪些档」的错误,别静默退化。
156
+ */
157
+ export function applyProfileTo(cfg, name) {
158
+ const profiles = cfg && cfg.profiles && typeof cfg.profiles === 'object' ? cfg.profiles : {};
159
+ const key = String(name || '').trim();
160
+ if (!key || !profiles[key] || typeof profiles[key] !== 'object') return null;
161
+ const o = { ...profiles[key] };
162
+ delete o['_说明'];
163
+ return { name: key, overrides: o };
164
+ }
165
+
129
166
  export function saveConfig(dataRoot, patch) {
130
167
  try {
131
168
  const p = configPath(dataRootOf(dataRoot));
@@ -148,6 +185,7 @@ function num(v, d) {
148
185
  /** 探一下服务在不在(1.5 秒超时,绝不阻塞消息链) */
149
186
  export async function health(cfg) {
150
187
  const c = cfg || loadConfig();
188
+ if (isComfy(c)) return comfyHealth(c);
151
189
  const url = String(c.baseUrl || '').replace(/\/+$/, '') + '/health';
152
190
  try {
153
191
  const ac = new AbortController();
@@ -164,6 +202,49 @@ export async function health(cfg) {
164
202
 
165
203
  const sleep = (ms) => new Promise((r) => setTimeout(r, ms));
166
204
 
205
+ const isComfy = (c) => String((c && c.backend) || '').toLowerCase() === 'comfyui';
206
+ const comfyBase = (c) => String((c && c.comfyUrl) || 'http://127.0.0.1:8191').replace(/\/+$/, '');
207
+
208
+ /** ComfyUI 版的探活:/system_stats 能回就算活着(模型是否在显存由它自己调度) */
209
+ async function comfyHealth(c) {
210
+ const base = comfyBase(c);
211
+ try {
212
+ const ac = new AbortController();
213
+ const t = setTimeout(() => ac.abort(), 2000);
214
+ const r = await fetch(base + '/system_stats', { signal: ac.signal });
215
+ clearTimeout(t);
216
+ if (!r.ok) return { ok: false, reason: 'http-' + r.status, baseUrl: base };
217
+ return {
218
+ ok: true, loaded: true, device: 'cuda', kind: 'comfyui',
219
+ model: String(c.comfyUnet || 'flux'), steps: num(c.comfySteps, 4), cfg: 1,
220
+ error: '', baseUrl: base,
221
+ };
222
+ } catch (e) {
223
+ return { ok: false, reason: e instanceof Error ? e.message : String(e), baseUrl: base };
224
+ }
225
+ }
226
+
227
+ /** ComfyUI 没起就用本地 bat 拉起来(窗口隐藏、detached,不随扩展卸载而死) */
228
+ async function ensureComfy(c, log) {
229
+ if ((await comfyHealth(c)).ok) return { ok: true, started: false, device: 'cuda' };
230
+ const bat = String(c.comfyStartBat || '');
231
+ if (!bat || !existsSync(bat)) return { ok: false, reason: 'comfy-down: ' + (bat || '(no comfyStartBat)') };
232
+ await freeLlm(c, log);
233
+ try {
234
+ log?.('info', `[draw] ComfyUI 没起,后台启动:${bat}`);
235
+ const child = spawn('cmd', ['/c', bat], { stdio: 'ignore', detached: true, windowsHide: true, env: process.env });
236
+ child.unref();
237
+ } catch (e) {
238
+ return { ok: false, reason: 'spawn-failed: ' + (e instanceof Error ? e.message : String(e)) };
239
+ }
240
+ const until = Date.now() + num(c.bootWaitMs, 120000);
241
+ while (Date.now() < until) {
242
+ await sleep(3000);
243
+ if ((await comfyHealth(c)).ok) return { ok: true, started: true, device: 'cuda' };
244
+ }
245
+ return { ok: false, reason: 'comfy-boot-timeout' };
246
+ }
247
+
167
248
  /** 服务里正跑着的是不是配置里这个模型(比 basename,忽略路径分隔符/大小写) */
168
249
  function sameModel(serverModel, wantModel) {
169
250
  const norm = (p) => String(p || '').replace(/\\/g, '/').toLowerCase().split('/').filter(Boolean).pop() || '';
@@ -197,6 +278,7 @@ async function freeLlm(cfg, log) {
197
278
  */
198
279
  export async function ensure(cfg, log) {
199
280
  const c = cfg || loadConfig();
281
+ if (isComfy(c)) return ensureComfy(c, log);
200
282
  const h = await health(c);
201
283
 
202
284
  // 换了模型(面板/配置里改了 modelDir)→ 把旧服务关掉重起,否则还在画旧模型
@@ -268,16 +350,32 @@ async function waitLoaded(c) {
268
350
  * @param {{width?:number,height?:number,steps?:number,seed?:number,out?:string,initImage?:string,strength?:number}} opts
269
351
  */
270
352
  export async function draw(prompt, opts = {}, cfg) {
271
- const c = { ...(cfg || loadConfig()), ...(opts.cfg || {}) };
353
+ // cfg 常被调用方当成「只需覆盖 dataRoot」的补丁传进来,所以先垫一层文件配置,
354
+ // 否则 backend/comfyUrl 这类键会被整个丢掉(曾导致 comfyui 后端不生效)。
355
+ const base = loadConfig((cfg && cfg.dataRoot) || (opts.cfg && opts.cfg.dataRoot));
356
+ const c = { ...base, ...(cfg || {}), ...(opts.cfg || {}) };
357
+ // 单次调用指定档位:opts.profile='qwen-image' 只影响这一次,不写回 draw-config.json
358
+ let usedProfile = String(c.activeProfile || '');
359
+ const wantProfile = String(opts.profile || (opts.cfg && opts.cfg.profile) || '').trim();
360
+ if (wantProfile) {
361
+ const picked = applyProfileTo(c, wantProfile);
362
+ if (!picked) return { ok: false, reason: 'unknown-profile: ' + wantProfile, profiles: listProfiles(c) };
363
+ Object.assign(c, picked.overrides);
364
+ usedProfile = picked.name;
365
+ }
272
366
  const text = String(prompt || '').trim().slice(0, num(c.maxPromptChars, 300));
273
367
  if (!text) return { ok: false, reason: 'empty-prompt' };
274
368
 
275
- const ready = await ensure(c);
276
- if (!ready.ok) return { ok: false, reason: 'server: ' + (ready.reason || '?') };
277
-
278
- const dir = join(String(c.dataRoot || dataRootOf(c)), '.qqbot', 'draw');
369
+ // 统一产出根(2026-11-28):所有产物只落 F:\qqbot\out\<image|video|audio|text>\<yyyy-MM>\
370
+ // 之前画图默认藏在 {dataRoot}/.qqbot/draw,主人根本找不到;现在和本地模型/SD/工具箱同一个出口。
371
+ const ym = new Date().toISOString().slice(0, 7);
372
+ const dir = join(__repoRoot(), 'out', 'image', ym);
279
373
  try { mkdirSync(dir, { recursive: true }); } catch { /* ignore */ }
280
374
  const out = opts.out || join(dir, `draw-${Date.now()}.png`);
375
+ if (isComfy(c)) return { ...(await drawComfy(text, opts, c, out)), profile: usedProfile };
376
+
377
+ const ready = await ensure(c);
378
+ if (!ready.ok) return { ok: false, reason: 'server: ' + (ready.reason || '?') };
281
379
 
282
380
  const negBase = String(c.negative || '');
283
381
  const negExtra = String(opts.negative || '');
@@ -312,9 +410,141 @@ export async function draw(prompt, opts = {}, cfg) {
312
410
  }
313
411
  }
314
412
 
413
+ /** FLUX GGUF 工作流(ComfyUI 自带 GGUF 节点;cfg 固定 1)。
414
+ * 蒸馏版 schnell 不吃 guidance/负面词;dev / krea 必须靠 FluxGuidance 节点注入(cfg 仍为 1),
415
+ * 否则 cfg=1 + 没 guidance 会出「灰蒙蒙、不听提示词」的图。comfyGuidance=0 表示不加该节点。
416
+ */
417
+ function comfyFluxWorkflow(c, prompt, negative, w, h, steps, seed, guidance = 0) {
418
+ const wf = {
419
+ '1': { class_type: 'UnetLoaderGGUF', inputs: { unet_name: String(c.comfyUnet || 'flux1-schnell-Q6_K.gguf') } },
420
+ '2': {
421
+ class_type: 'DualCLIPLoaderGGUF',
422
+ inputs: {
423
+ clip_name1: String(c.comfyClipL || 'clip_l.safetensors'),
424
+ clip_name2: String(c.comfyT5 || 't5-v1_1-xxl-encoder-Q5_K_M.gguf'),
425
+ type: 'flux',
426
+ },
427
+ },
428
+ '3': { class_type: 'VAELoader', inputs: { vae_name: String(c.comfyVae || 'ae.safetensors') } },
429
+ '4': { class_type: 'CLIPTextEncode', inputs: { clip: ['2', 0], text: prompt } },
430
+ '5': { class_type: 'CLIPTextEncode', inputs: { clip: ['2', 0], text: String(negative || '') } },
431
+ '6': { class_type: 'EmptySD3LatentImage', inputs: { width: w, height: h, batch_size: 1 } },
432
+ '7': {
433
+ class_type: 'KSampler',
434
+ inputs: {
435
+ model: ['1', 0], positive: ['4', 0], negative: ['5', 0], latent_image: ['6', 0],
436
+ seed, steps, cfg: 1, sampler_name: 'euler', scheduler: 'simple', denoise: 1,
437
+ },
438
+ },
439
+ '8': { class_type: 'VAEDecode', inputs: { samples: ['7', 0], vae: ['3', 0] } },
440
+ '9': { class_type: 'SaveImage', inputs: { images: ['8', 0], filename_prefix: 'qqbot/draw' } },
441
+ };
442
+ if (num(guidance, 0) > 0) {
443
+ wf['10'] = { class_type: 'FluxGuidance', inputs: { conditioning: ['4', 0], guidance: num(guidance, 0) } };
444
+ wf['7'].inputs.positive = ['10', 0];
445
+ }
446
+ return wf;
447
+ }
448
+
449
+ /** Qwen-Image-2512 工作流(comfyArch=qwen 时用)。
450
+ * 和 FLUX 的三处硬区别,别混:①文本塔 CLIPLoader 的 type 必须是 'qwen_image'(不是 flux);
451
+ * ②UNET 后必须挂 ModelSamplingAuraFlow(shift≈3.1),否则画面发灰/过饱和;
452
+ * ③它是真 CFG 模型(2.5~3.0),走 cfg 而不是 FluxGuidance。
453
+ */
454
+ function comfyQwenWorkflow(c, prompt, negative, w, h, steps, seed, cfg = 2.5) {
455
+ return {
456
+ '1': { class_type: 'UnetLoaderGGUF', inputs: { unet_name: String(c.comfyUnet || 'qwen-image-2512-Q4_K_M.gguf') } },
457
+ '2': {
458
+ class_type: 'CLIPLoader',
459
+ inputs: { clip_name: String(c.comfyTe || 'qwen_2.5_vl_7b_fp8_scaled.safetensors'), type: 'qwen_image', device: 'default' },
460
+ },
461
+ '3': { class_type: 'VAELoader', inputs: { vae_name: String(c.comfyVae || 'qwen_image_vae.safetensors') } },
462
+ '4': { class_type: 'CLIPTextEncode', inputs: { clip: ['2', 0], text: prompt } },
463
+ '5': { class_type: 'CLIPTextEncode', inputs: { clip: ['2', 0], text: String(negative || '') } },
464
+ '6': { class_type: 'EmptySD3LatentImage', inputs: { width: w, height: h, batch_size: 1 } },
465
+ '7': { class_type: 'ModelSamplingAuraFlow', inputs: { model: ['1', 0], shift: num(c.comfyShift, 3.1) } },
466
+ '8': {
467
+ class_type: 'KSampler',
468
+ inputs: {
469
+ model: ['7', 0], positive: ['4', 0], negative: ['5', 0], latent_image: ['6', 0],
470
+ seed, steps, cfg: num(cfg, 2.5), sampler_name: 'euler', scheduler: 'simple', denoise: 1,
471
+ },
472
+ },
473
+ '9': { class_type: 'VAEDecode', inputs: { samples: ['8', 0], vae: ['3', 0] } },
474
+ '10': { class_type: 'SaveImage', inputs: { images: ['9', 0], filename_prefix: 'qqbot/draw' } },
475
+ };
476
+ }
477
+
478
+ /** 走 ComfyUI 出图:提交 → 轮询 /history → 取回 /view 落盘 */
479
+ async function drawComfy(text, opts, c, out) {
480
+ const base = comfyBase(c);
481
+ const ready = await ensureComfy(c);
482
+ if (!ready.ok) return { ok: false, reason: 'server: ' + (ready.reason || '?') };
483
+ // Ollama 常驻 ~5G,FLUX 要 ~11G,先把它请下去
484
+ await freeLlm(c);
485
+
486
+ const snap = (n, d) => {
487
+ const v = Math.round(num(n, d) / 16) * 16;
488
+ return Math.max(256, Math.min(1536, v));
489
+ };
490
+ const w = snap(opts.width, num(c.width, 1024));
491
+ const h = snap(opts.height, num(c.height, 1024));
492
+ const steps = Math.max(1, Math.min(30, Math.round(num(opts.steps, num(c.comfySteps, 4)))));
493
+ const seed = num(opts.seed, 0) > 0 ? Math.round(num(opts.seed, 0)) : Math.floor(Math.random() * 2 ** 31);
494
+ const guidance = num(opts.guidance, num(c.comfyGuidance, 0));
495
+ // comfyArch: flux(默认) | qwen —— Qwen-Image 走独立工作流(真 CFG,不用 FluxGuidance)
496
+ const isQwen = String(c.comfyArch || 'flux').toLowerCase() === 'qwen';
497
+ const wf = isQwen
498
+ ? comfyQwenWorkflow(c, text + String(c.styleSuffix || ''), String(c.negative || ''), w, h, steps, seed, num(opts.cfg, num(c.comfyCfg, 2.5)))
499
+ : comfyFluxWorkflow(c, text + String(c.styleSuffix || ''), String(c.negative || ''), w, h, steps, seed, guidance);
500
+
501
+ const t0 = Date.now();
502
+ let pid = '';
503
+ try {
504
+ const r = await fetch(base + '/prompt', {
505
+ method: 'POST', headers: { 'content-type': 'application/json' },
506
+ body: JSON.stringify({ prompt: wf, client_id: 'qqbot-draw-' + t0 }),
507
+ });
508
+ const j = await r.json().catch(() => ({}));
509
+ pid = String(j?.prompt_id || '');
510
+ if (!pid) return { ok: false, reason: 'comfy-reject: ' + JSON.stringify(j).slice(0, 220) };
511
+ } catch (e) {
512
+ return { ok: false, reason: 'comfy-request-failed: ' + (e instanceof Error ? e.message : String(e)) };
513
+ }
514
+
515
+ const deadline = t0 + num(c.timeoutMs, 180000);
516
+ let img = null;
517
+ while (Date.now() < deadline && !img) {
518
+ await sleep(1200);
519
+ try {
520
+ const r = await fetch(base + '/history/' + pid);
521
+ const j = await r.json();
522
+ const hist = j?.[pid];
523
+ const outs = (hist && hist.outputs) || {};
524
+ for (const node of Object.keys(outs)) {
525
+ const arr = outs[node]?.images;
526
+ if (Array.isArray(arr) && arr[0]?.filename) { img = arr[0]; break; }
527
+ }
528
+ } catch { /* 继续轮询 */ }
529
+ }
530
+ if (!img) return { ok: false, reason: 'comfy-timeout(' + (Date.now() - t0) + 'ms)' };
531
+
532
+ try {
533
+ const q = new URLSearchParams({ filename: img.filename, subfolder: img.subfolder || '', type: img.type || 'output' });
534
+ const got = await fetch(base + '/view?' + q.toString());
535
+ if (!got.ok) return { ok: false, reason: 'comfy-view-http-' + got.status };
536
+ const buf = Buffer.from(await got.arrayBuffer());
537
+ writeFileSync(out, buf);
538
+ return { ok: true, path: out, ms: Date.now() - t0, seed, bytes: buf.length, device: 'cuda' };
539
+ } catch (e) {
540
+ return { ok: false, reason: 'comfy-save-failed: ' + (e instanceof Error ? e.message : String(e)) };
541
+ }
542
+ }
543
+
315
544
  /** 关掉服务(换了模型 / 要省显存时用) */
316
545
  export async function stopServer(cfg) {
317
546
  const c = cfg || loadConfig();
547
+ if (isComfy(c)) return false; // ComfyUI 是公用服务,不随出图停
318
548
  try {
319
549
  const ac = new AbortController();
320
550
  const t = setTimeout(() => ac.abort(), 1500);
@@ -389,8 +619,9 @@ export async function status(cfg) {
389
619
  const h = await health(c);
390
620
  return {
391
621
  enabled: c.enabled !== false,
392
- baseUrl: c.baseUrl,
393
- modelDir: c.modelDir,
622
+ backend: isComfy(c) ? 'comfyui' : 'sdserver',
623
+ baseUrl: isComfy(c) ? comfyBase(c) : c.baseUrl,
624
+ modelDir: isComfy(c) ? String(c.comfyUnet || '') : c.modelDir,
394
625
  up: h.ok === true,
395
626
  loaded: h.loaded === true,
396
627
  device: h.device || '',