Files
dsh-comfyui/lib/client.js
T
2026-11-23 09:49:57 +08:00

184 lines
9.3 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
window.__ModuleLoader__.load({
id: '@wangjinding/dsh-comfyui',
factory(require) {
const React = require('react');
const h = React.createElement;
const { useState, useEffect, useCallback } = React;
const API = '/plugin/comfyui';
const T = {
text: 'var(--dsw-alias-label-primary, #1f2329)',
sub: 'var(--dsw-alias-label-secondary, #646a73)',
dim: 'var(--dsw-alias-label-tertiary, #8f959e)',
line: 'var(--dsw-alias-border-l2, #dee0e3)',
card: 'var(--dsw-alias-bg-layer-2, #ffffff)',
field: 'var(--dsw-alias-bg-layer-1, #f7f8fa)',
brand: 'var(--dsw-alias-brand-primary, #247bbf)',
ok: 'var(--dsw-alias-state-success-primary, #2ea121)',
err: 'var(--dsw-alias-state-error-primary, #f54a45)',
};
const css = {
wrap: { padding: '4px 0 24px', color: T.text, fontSize: 14, lineHeight: 1.6 },
h1: { fontSize: 17, fontWeight: 600, margin: '0 0 6px' },
desc: { color: T.sub, margin: '0 0 18px', fontSize: 13 },
card: { border: `1px solid ${T.line}`, borderRadius: 10, background: T.card, padding: 16, marginBottom: 14 },
label: { display: 'block', fontSize: 13, fontWeight: 600, marginBottom: 8, whiteSpace: 'nowrap', flexShrink: 0 },
row: { display: 'flex', gap: 8, alignItems: 'center', flexWrap: 'wrap' },
input: {
flex: '1 1 320px', minWidth: 220, height: 34, padding: '0 10px', borderRadius: 8,
border: `1px solid ${T.line}`, background: T.field, color: T.text, fontSize: 13,
fontFamily: 'ui-monospace, SFMono-Regular, Menlo, monospace', outline: 'none',
},
btn: { height: 34, padding: '0 14px', borderRadius: 8, cursor: 'pointer', border: `1px solid ${T.line}`, background: T.card, color: T.text, fontSize: 13 },
primary: { border: `1px solid ${T.brand}`, background: T.brand, color: '#fff', fontWeight: 600 },
hint: { marginTop: 10, fontSize: 12, color: T.dim },
status: { display: 'inline-flex', alignItems: 'center', gap: 6, whiteSpace: 'nowrap', flexShrink: 0, padding: '3px 10px', borderRadius: 999, fontSize: 12, fontWeight: 600 },
kv: { display: 'grid', gridTemplateColumns: 'auto 1fr', gap: '6px 14px', fontSize: 13, marginTop: 12 },
k: { color: T.dim },
bullet: { margin: '6px 0 0', paddingLeft: 18, color: T.sub, fontSize: 13 },
kbd: { fontFamily: 'ui-monospace, SFMono-Regular, Menlo, monospace', background: T.field, border: `1px solid ${T.line}`, borderRadius: 4, padding: '0 4px', fontSize: 12 },
pre: { margin: '10px 0 0', padding: 10, borderRadius: 8, background: T.field, fontSize: 12, color: T.sub, whiteSpace: 'pre-wrap', wordBreak: 'break-all', fontFamily: 'ui-monospace, monospace' },
};
async function call(path, init) {
const resp = await fetch(`${API}${path}`, { ...init, headers: { 'Content-Type': 'application/json', ...(init && init.headers) } });
const body = await resp.json().catch(() => ({}));
if (!resp.ok || body.ok === false) throw new Error(body.error || `HTTP ${resp.status}`);
return body;
}
function Dot({ on }) {
return h('span', { style: { width: 6, height: 6, borderRadius: 999, background: on ? T.ok : T.err, display: 'inline-block' } });
}
function ComfyPanel() {
const [info, setInfo] = useState(null);
const [url, setUrl] = useState('');
const [busy, setBusy] = useState('');
const [msg, setMsg] = useState(null);
const refresh = useCallback(async (silent) => {
try {
const s = await call('/status', { method: 'GET' });
setInfo(s);
setUrl((cur) => cur || s.baseUrl || '');
} catch (e) {
if (!silent) setMsg({ kind: 'err', text: `读取状态失败:${e.message}` });
}
}, []);
useEffect(() => { refresh(true); }, [refresh]);
const save = async (withTest) => {
setBusy(withTest ? 'save' : 'saveonly'); setMsg(null);
try {
await call('/config', { method: 'POST', body: JSON.stringify({ baseUrl: url }) });
setMsg({ kind: 'ok', text: '地址已保存' });
await refresh(true);
if (withTest) {
setBusy('test');
const t = await call('/test', { method: 'POST', body: '{}' });
setInfo(t);
setMsg({ kind: 'ok', text: `连接正常:ComfyUI ${t.version},${t.devices?.[0]?.name || '未知设备'}` });
}
} catch (e) {
setMsg({ kind: 'err', text: `失败:${e.message}` });
} finally { setBusy(''); }
};
const test = async () => {
setBusy('test'); setMsg(null);
try {
const t = await call('/test', { method: 'POST', body: '{}' });
setInfo(t);
setMsg({ kind: 'ok', text: `连接正常(${t.devices?.[0]?.name || 'GPU 未知'},空闲 ${t.devices?.[0]?.vramFreeGB ?? '?'}GB)` });
} catch (e) {
setMsg({ kind: 'err', text: `连不上:${e.message}` });
} finally { setBusy(''); }
};
const reachable = !!info?.reachable;
const dev = info?.devices?.[0];
return h('div', { style: css.wrap },
h('h1', { style: css.h1 }, 'ComfyUI'),
h('p', { style: css.desc },
'接入局域网里的 ComfyUI 后,DSH 可以直接用它的显卡出图、跑视频工作流。工具:',
h('code', { style: css.kbd }, 'comfy_generate_image'),
' / ',
h('code', { style: css.kbd }, 'comfy_run_workflow'),
' / ',
h('code', { style: css.kbd }, 'comfy_upload'),
' / ',
h('code', { style: css.kbd }, 'comfy_status')),
h('div', { style: css.card },
h('div', { style: { display: 'flex', alignItems: 'center', gap: 10, marginBottom: 12, flexWrap: 'wrap' } },
h('span', { style: css.label, marginBottom: 0 }, '服务地址'),
h('span', { style: { ...css.status, background: reachable ? 'rgba(46,161,33,.12)' : 'rgba(245,74,69,.12)', color: reachable ? T.ok : T.err } },
h(Dot, { on: reachable }),
reachable ? `已连接 · v${info.version || '?'}` : '未连接')),
h('div', { style: css.row },
h('input', {
style: css.input, value: url, spellCheck: false,
placeholder: 'http://<ComfyUI 主机>:8188',
onChange: (e) => setUrl(e.target.value),
onKeyDown: (e) => { if (e.key === 'Enter' && !busy) save(true); },
}),
h('button', { style: { ...css.btn, ...css.primary, opacity: busy ? 0.6 : 1 }, disabled: !!busy, onClick: () => save(true) },
busy === 'save' ? '连接中…' : '保存并连接'),
h('button', { style: { ...css.btn, opacity: busy ? 0.6 : 1 }, disabled: !!busy, onClick: test },
busy === 'test' ? '测试中…' : '测试连接')),
msg ? h('div', { style: { marginTop: 12, fontSize: 13, color: msg.kind === 'ok' ? T.ok : T.err } }, msg.text) : null,
reachable && info ? h('div', { style: css.kv },
h('span', { style: css.k }, 'GPU'),
h('span', null, `${dev?.name || '未知'}|显存 ${dev?.vramTotalGB ?? '?'}GB(空闲 ${dev?.vramFreeGB ?? '?'}GB)`),
h('span', { style: css.k }, '内存'),
h('span', null, `${info.ramTotalGB ?? '?'} GB`),
h('span', { style: css.k }, '队列'),
h('span', null, `${info.queue?.running ?? 0} 运行 / ${info.queue?.pending ?? 0} 排队`),
h('span', { style: css.k }, '输出目录'),
h('span', { style: { wordBreak: 'break-all' } }, info.outputDir || '—'),
h('span', { style: css.k }, '内置模型'),
h('span', null, `${info.models?.unet || '—'}(${info.models?.clip || '—'} / ${info.models?.vae || '—'})`))
: null,
!reachable && info?.error
? h('div', { style: css.pre }, info.error)
: null,
h('p', { style: css.hint },
'ComfyUI 默认没有认证,且通常监听 0.0.0.0 —— 同一局域网内任何设备都能连上并占用显卡。建议只在家里内网使用。')),
h('div', { style: css.card },
h('div', { style: css.label }, '能做什么'),
h('ul', { style: css.bullet },
h('li', null, h('b', null, '出图'), ':内置 Z-Image Turbo 工作流,1024² / 8 步约 25 秒(本机实测)'),
h('li', null, h('b', null, '跑任意工作流'), ':把 ComfyUI 界面里「导出 (API)」得到的 JSON 交给 ',
h('code', { style: css.kbd }, 'comfy_run_workflow'), ',产物自动下载到本地'),
h('li', null, h('b', null, '本地图做输入'), ':先用 ',
h('code', { style: css.kbd }, 'comfy_upload'), ' 传到 ComfyUI 的 input 目录,再在 workflow 里引用'),
h('li', null, h('b', null, '视频 / 音频'), ':这台装了 LTX 2.5、MiniMax H3、Stable Audio 3,导出对应 API 工作流即可跑')),
h('p', { style: css.hint }, '设置保存在 ~/.dsh/storages/dsh-comfyui/config.json,不写进 profile 配置。')));
}
return {
inject: ['slots'],
apply(ctx) {
ctx.effect(() => ctx.slots.inject('settings.section', () => ctx.slots.register({
name: 'settings.section',
id: 'comfyui',
order: 15,
label: () => 'ComfyUI',
inject: () => ({}),
}, ComfyPanel)), 'comfyui: settings section');
},
};
},
});