fix(send): name ComfyUI windows by their tab and explain why a send is blocked (#539)

Closes #535
This commit is contained in:
Vito Sansevero
2026-10-30 16:52:41 -07:00
parent e6e425ed35
commit 07c2442e23
23 changed files with 376 additions and 57 deletions
+10 -2
View File
@@ -12,6 +12,7 @@ const compositionNames = ['prepend_text','append_text'];
function nodes(graph) { return graph?._nodes || []; }
function target(node) { return {id:String(node.id),label:String(node.title || node.type),type:node.type}; }
function connected(node,name) { return Boolean(node.inputs?.some(input => input.name === name && input.link != null)); }
function linkedRoles(node) { return Object.entries(schemas.get(node.type)).filter(([,name]) => connected(node,name)).map(([role]) => role); }
function links(node) { return JSON.stringify((node.inputs || []).map(input => [input.name,input.link ?? null])); }
function capture(node,name) {
const widget = node.widgets?.find(item => item.name === name);
@@ -57,9 +58,16 @@ function renderer(runtime) {
export function createPromptAdapter(app, {runtime = globalThis} = {}) {
const snapshots = new WeakMap();
// Loading a workflow makes new node objects but can land on the same graph version, so a revision needs both.
const nodeSerials = new WeakMap();
let lastSerial = 0;
function revision(node) {
if (!nodeSerials.has(node)) nodeSerials.set(node,++lastSerial);
return `${nodeSerials.get(node)}:${app.graph?._version}`;
}
function listTargets() {
const drawn = renderer(runtime);
return nodes(app.graph).filter(node => schemas.has(node.type)).map(node => ({...target(node),roles:Object.keys(schemas.get(node.type)),...drawn}));
return nodes(app.graph).filter(node => schemas.has(node.type)).map(node => ({...target(node),roles:Object.keys(schemas.get(node.type)),linkedRoles:linkedRoles(node),revision:revision(node),...drawn}));
}
function preview(targetId,rawRoles) {
const roles = validateRoles(rawRoles);
@@ -77,7 +85,7 @@ export function createPromptAdapter(app, {runtime = globalThis} = {}) {
});
const snapshot = Object.freeze({});
snapshots.set(snapshot,{graph,node,type:node.type,id:String(node.id),version:graph._version,widgets:node.widgets,widgetOrder:[...node.widgets],fields,links:links(node),requested});
return {preview:{target:target(node),fields:requested.map(({role,after,field}) => ({role,before:String(field.value ?? ''),after})),composition:composition(node,fields,roles)},snapshot};
return {preview:{target:target(node),revision:revision(node),fields:requested.map(({role,after,field}) => ({role,before:String(field.value ?? ''),after})),composition:composition(node,fields,roles)},snapshot};
}
function apply(snapshot) {
const state = snapshots.get(snapshot);
+2 -1
View File
@@ -16,7 +16,8 @@ export function installPromptBridge(app, { baseUrl, runtime = globalThis,
if (host || disposed) return;
try {
host = hostFactory({ namespace, origin: runtime.location.origin,
label: `PromptManager — ${runtime.document.title || 'ComfyUI'}`, adapter: adapterFactory(app) });
// A getter: the Toolkit reads this on every discovery, and the tab title follows the open workflow.
get label() { return runtime.document.title || 'ComfyUI'; }, adapter: adapterFactory(app) });
} catch (error) {
runtime.console.warn('[PromptManager] Prompt sending is unavailable:', error.message);
}
+84 -18
View File
@@ -1,51 +1,117 @@
import {createBridgeClient, validateRoles} from '../shared/toolkit/prompt-bridge.js';
import {createButton, createField, createScope, createSelect} from '../shared/toolkit/core.js';
// The same wait as System's renderer line, so a busy tab is not reported as missing.
const DISCOVERY_WAIT_MS = 300;
const roleNames = roles => Object.keys(roles).join(' and ');
const roleOf = target => Array.isArray(target.roles) ? target.roles : [];
const accepts = (target,roles) => typeof target.id==='string' && Object.keys(roles).every(role=>roleOf(target).includes(role));
const linkedTo = (target,roles) => Object.keys(roles).filter(role=>Array.isArray(target.linkedRoles) && target.linkedRoles.includes(role));
const nodeLabel = target => `#${target.id} ${target.label}`;
// A lost reply leaves the outcome unknown; anything else the host said is a refusal, and nothing was written.
const LOST_REPLY = /timed out|aborted|disposed/i;
/** Why nothing in the discovered windows can receive these roles, most specific first (D173). */
function unavailableMessage(found,roles) {
if (!found.length) return 'No ComfyUI window answered in this browser at this address. Open ComfyUI here, then find windows again.';
for (const host of found) for (const target of host.targets) {
const linked = accepts(target,roles) ? linkedTo(target,roles) : [];
if (linked.length) return `${nodeLabel(target)} in ${host.label} has its ${linked.join(' and ')} prompt linked to another node, so it cannot receive text. Disconnect the link, then find windows again.`;
}
const where = found.length===1 ? found[0].label : `None of the ${found.length} ComfyUI windows`;
const verb = found.length===1 ? 'has no' : 'has a';
if (found.some(host=>host.targets.length)) {
const need = Object.hasOwn(roles,'negative') ? ' Add a Prompt Manager (+/-) node, then find windows again.' : ' Add one, then find windows again.';
return `${where} ${verb} Prompt Manager node that accepts a ${roleNames(roles)} prompt.${need}`;
}
return `${where} ${verb} Prompt Manager node. Add one to the workflow, then find windows again.`;
}
/** Keeps same-named windows apart: tabs on one workflow. */
function distinctLabels(items,suffix) {
const seen = new Map();
const totals = new Map();
for (const item of items) totals.set(item.label,(totals.get(item.label) || 0)+1);
return items.map(item => {
const count = (seen.get(item.label) || 0)+1;seen.set(item.label,count);
return totals.get(item.label)>1 ? {...item,label:suffix(item,count)} : item;
});
}
export function createSendSession({client, roles:rawRoles, onChange}) {
const roles = Object.freeze(validateRoles(rawRoles));
let state = {phase:'idle',hosts:[],hostId:'',targetId:'',preview:null,message:''};
let state = {phase:'idle',hosts:[],hostId:'',targetId:'',preview:null,outcomeUnknown:false,message:''};
let operation, generation = 0, disposed = false;
const emit = patch => { if (!disposed) { state = Object.freeze({...state,...patch}); onChange(state); } };
function invalidate() { generation++; operation?.abort(); operation = null; }
function begin(phase) { invalidate(); operation = new AbortController(); emit({phase,preview:null,message:''}); return {signal:operation.signal,version:generation}; }
function begin(phase) { invalidate(); operation = new AbortController(); emit({phase,preview:null,outcomeUnknown:false,message:''}); return {signal:operation.signal,version:generation}; }
const current = request => !disposed && generation === request.version;
const selected = () => state.hosts.find(host=>host.hostId===state.hostId)?.targets.find(target=>target.id===state.targetId);
async function fetchPreview(request) {
const preview=await client.preview(state.hostId,state.targetId,roles,{signal:request.signal});
if (!current(request)) return null;
const fields=preview?.fields;
if(typeof preview?.ticket!=='string' || !preview.ticket || preview.target?.id!==state.targetId || !Array.isArray(fields) || fields.length!==Object.keys(roles).length || new Set(fields.map(field=>field?.role)).size!==fields.length || Array.from(fields).some(field=>!field || !Object.hasOwn(roles,field.role) || typeof field.before!=='string' || field.after!==roles[field.role])) throw Error('ComfyUI returned an unexpected preview. Find the destination and preview again.');
// Undefined on a tab still running older code, which has nothing to compare.
const found=selected()?.revision;
if(found!==undefined && preview.revision!==found) throw Error('The workflow changed since this window was found. Nothing was sent. Find windows again.');
return preview;
}
function hostChoice(host) {
if (host.targets.length>1) return {hostId:host.hostId,targetId:'',message:`Choose a destination node in ${host.label}.`};
const [target] = host.targets;
return {hostId:host.hostId,targetId:target.id,message:`Sending to ${target.label} in ${host.label}. Apply to send it, or preview the changes first.`};
}
return {
get state() { return state; },
async discover() {
if (disposed) return;
const request = begin('discovering'); emit({hosts:[],hostId:'',targetId:'',message:'Looking for ComfyUI windows…'});
try {
const found = await client.discover({signal:request.signal});
const found = (await client.discover({signal:request.signal,waitMs:DISCOVERY_WAIT_MS})).map(host=>({...host,targets:host.targets.filter(target=>target && typeof target==='object')}));
if (!current(request)) return;
const hosts = found.map(host=>({...host,targets:host.targets.filter(target=>typeof target.id==='string' && Array.isArray(target.roles) && Object.keys(roles).every(role=>target.roles.includes(role)))})).filter(host=>host.targets.length);
emit({phase:'select',hosts,message:hosts.length ? 'Choose a ComfyUI window and destination node.' : 'No compatible ComfyUI windows found. Open a workflow with a compatible Prompt Manager node, then find windows again.'});
const hosts = distinctLabels(found.toSorted((a,b)=>a.hostId<b.hostId?-1:1).map(host=>({...host,targets:host.targets.filter(target=>accepts(target,roles) && !linkedTo(target,roles).length).map(target=>({...target,label:nodeLabel(target)}))})).filter(host=>host.targets.length),(host,count)=>count>1 ? `${host.label} (window ${count})` : host.label);
// Nodes left out are counted so an auto-selected destination never hides the one the user meant.
const skipped = found.reduce((total,host)=>total+host.targets.length,0)-hosts.reduce((total,host)=>total+host.targets.length,0);
const note = skipped ? ` ${skipped} other Prompt Manager ${skipped===1?'node':'nodes'} cannot receive this prompt.` : '';
if (!hosts.length) emit({phase:'select',hosts,message:unavailableMessage(found,roles)});
else if (hosts.length>1) emit({phase:'select',hosts,message:`Choose a ComfyUI window.${note}`});
else { const choice=hostChoice(hosts[0]); emit({phase:'select',hosts,...choice,message:choice.message+note}); }
} catch (error) { if (current(request)) emit({phase:'error',message:error.message}); }
},
selectHost(hostId) { if (disposed) return; invalidate(); emit({phase:'select',hostId:state.hosts.some(host=>host.hostId===hostId)?hostId:'',targetId:'',preview:null,message:'Choose a destination node, then preview changes.'}); },
selectTarget(targetId) { if (disposed) return; invalidate(); const host=state.hosts.find(host=>host.hostId===state.hostId); emit({phase:'select',targetId:host?.targets.some(target=>target.id===targetId)?targetId:'',preview:null,message:'Preview changes before applying.'}); },
selectHost(hostId) { if (disposed) return; invalidate(); const host=state.hosts.find(host=>host.hostId===hostId); emit({phase:'select',preview:null,outcomeUnknown:false,...(host ? hostChoice(host) : {hostId:'',targetId:'',message:'Choose a ComfyUI window.'})}); },
selectTarget(targetId) { if (disposed) return; invalidate(); const host=state.hosts.find(host=>host.hostId===state.hostId); const chosen=host?.targets.some(target=>target.id===targetId); emit({phase:'select',outcomeUnknown:false,targetId:chosen?targetId:'',preview:null,message:chosen?'Apply to send it, or preview the changes first.':'Choose a destination node, then preview changes.'}); },
async preview() {
if (disposed || !selected() || ['previewing','applying'].includes(state.phase)) return;
const request=begin('previewing');
try {
const preview=await client.preview(state.hostId,state.targetId,roles,{signal:request.signal});
if (!current(request)) return;
const fields=preview?.fields;
if(typeof preview?.ticket!=='string' || !preview.ticket || preview.target?.id!==state.targetId || !Array.isArray(fields) || fields.length!==Object.keys(roles).length || new Set(fields.map(field=>field?.role)).size!==fields.length || Array.from(fields).some(field=>!field || !Object.hasOwn(roles,field.role) || typeof field.before!=='string' || field.after!==roles[field.role])) throw Error('ComfyUI returned an unexpected preview. Find the destination and preview again.');
emit({phase:'ready',preview,message:'Review the complete changes, then apply.'});
const preview=await fetchPreview(request);
if (!preview) return;
const windowLabel=state.hosts.find(host=>host.hostId===state.hostId)?.label;
emit({phase:'ready',preview,message:`Sending to ${nodeLabel({id:preview.target.id,label:preview.target.label ?? ''})} in ${windowLabel}. Review the complete changes, then apply.`});
} catch(error) { if(current(request)) emit({phase:'error',preview:null,message:error.message}); }
},
async apply() {
if(disposed || state.phase!=='ready' || !state.preview) return;
const {ticket}=state.preview; const targetId=state.targetId; const hostId=state.hostId;
// Success stays closed: the second click of a double click must not send again.
if(disposed || !selected() || !['select','ready','error'].includes(state.phase) || state.outcomeUnknown) return;
const held=state.phase==='ready' ? state.preview : null;
const targetId=state.targetId; const hostId=state.hostId;
const request=begin('applying');
emit({message:'Waiting for ComfyUI confirmation. Closing this dialog does not undo a sent update.'});
let posted=false;
try {
const acknowledgement=await client.apply(hostId,ticket,{signal:request.signal});
const preview=held ?? await fetchPreview(request);
if(!preview || !current(request)) return;
posted=true;
const acknowledgement=await client.apply(hostId,preview.ticket,{signal:request.signal});
if(!current(request)) return;
if(acknowledgement?.targetId!==targetId || !acknowledgement.values || Object.keys(acknowledgement.values).length!==Object.keys(roles).length || Object.keys(roles).some(role=>!Object.hasOwn(acknowledgement.values,role) || acknowledgement.values[role]!==roles[role])) throw Error('ComfyUI returned an unexpected acknowledgement.');
emit({phase:'success',message:'Sent to ComfyUI.'});
} catch(error) { if(current(request)) emit({phase:'error',preview:null,message:`No confirmation received. The update may have applied. Check ComfyUI, then preview again. ${error.message}`}); }
} catch(error) {
if(!current(request)) return;
const unknown=posted && (LOST_REPLY.test(error.message) || /unexpected acknowledgement/.test(error.message));
emit({phase:'error',preview:null,outcomeUnknown:unknown,message:unknown ? `No confirmation received. The update may have applied. Check ComfyUI, then preview again. ${error.message}` : error.message});
}
},
dispose() { if(disposed) return; disposed=true; invalidate(); client.dispose(); },
};
@@ -82,7 +148,7 @@ export function createPromptSender({scope,baseUrl=window.ApiClient.resolveUrl('/
const header=element('header',undefined,'prompt-sender-header');
const title=element('h2','Send to ComfyUI');const dismiss=createButton('Close');
header.append(title,dismiss);
const description=element('p','Choose where to send this prompt. Nothing changes until you approve the preview.');
const description=element('p','Choose where to send this prompt. Apply writes it to that node. Preview changes shows the result first, if you want to check it.');
const status=element('p','', 'prompt-sender-status');status.setAttribute('role','status');status.setAttribute('aria-live','polite');
const host=createSelect([['','Choose a ComfyUI window']], '');
const target=createSelect([['','Choose a destination node']], '');
@@ -123,7 +189,7 @@ export function createPromptSender({scope,baseUrl=window.ApiClient.resolveUrl('/
const selected=state.hosts.find(item=>item.hostId===state.hostId);
options(target,'Choose a destination node',(selected?.targets || []).map(item=>[item.id,item.label]),state.targetId);
host.disabled=busy;target.disabled=busy || !selected;
find.disabled=busy;inspect.disabled=busy || !state.targetId;apply.disabled=state.phase!=='ready';
find.disabled=busy;inspect.disabled=busy || !state.targetId;apply.disabled=busy || !state.targetId || state.phase==='success' || state.outcomeUnknown;
preview.replaceChildren();
if(state.preview) {
preview.append(element('h3','Review changes'));
+2 -2
View File
@@ -2,7 +2,7 @@
* System › Integrations (#249 System S10 Task 27): the concept's `renderSystem` "integrations" body (app.js 682–684).
*
* - "ComfyUI": Default send destination, Host compatibility and Return to canvas after send. D106 (Q8): the send
* bridge always asks for the window and node, and the nodes always follow ComfyUI's own renderer, so only "Ask each
* bridge asks for the window and node when there is a choice, and the nodes always follow ComfyUI's own renderer, so only "Ask each
* time" and "Follow host renderer" can be chosen; the other options and the return toggle are disabled, with the
* reason under the row, and nothing is stored for them. Host compatibility also says which renderer the open
* ComfyUI windows use, as the send bridge reports it (`renderer` on each Prompt Manager node it lists).
@@ -42,7 +42,7 @@ export const LORA_KEYS = Object.freeze(['lora_enabled', 'lora_trigger_words_enab
/** D106: why the options the bridge cannot honour are disabled. */
export const REASONS = Object.freeze({
send: 'Sending always asks for the ComfyUI window and node, so “Ask each time” is the only option.',
send: 'Sending asks when there is more than one ComfyUI window or node, so “Ask each time” is the only option.',
renderer: 'Prompt Manager nodes always follow ComfyUI’s own renderer setting.',
returnCanvas: 'Not available: sending applies the text in ComfyUI and keeps this page open.',
});