Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 44 additions & 0 deletions electron/main/extension-install-utils.test.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -529,3 +529,47 @@ test('parseGitHubRepoUrl rejects URLs that do not name a GitHub repository', ()
assert.throws(() => mod.parseGitHubRepoUrl('https://github.com/owner/.git'), /expected format/)
})


test('normalizeNodeInputs reduces slot objects to type names and keeps labels and optionality (#241)', () => {
const mod = loadModule()
// The manifest shape from the report (modly-Codex-image-extension, image-to-image)
assert.deepEqual(mod.normalizeNodeInputs({
id: 'image-to-image',
input: 'image',
inputs: [
{ name: 'front', label: 'Primary image', type: 'image', required: true },
{ name: 'left', label: 'Image 2', type: 'image', required: false },
],
}), {
inputs: ['image', 'image'],
inputLabels: ['Primary image', 'Image 2'],
inputsRequired: [true, false],
})

// Plain type names are unchanged, with no derived labels or optionality
assert.deepEqual(
mod.normalizeNodeInputs({ inputs: ['image', 'text'], input_labels: ['Image', 'Prompt'] }),
{ inputs: ['image', 'text'], inputLabels: ['Image', 'Prompt'], inputsRequired: undefined },
)
// Explicit input_labels win; a slot without a label falls back to its name, then its type
assert.deepEqual(
mod.normalizeNodeInputs({ inputs: ['mesh', { type: 'image', name: 'ref' }] }).inputLabels,
['mesh', 'ref'],
)
assert.deepEqual(mod.normalizeNodeInputs({ input: 'image' }), { inputLabels: undefined })
})

test('normalizeNodeInputs rejects slot entries without a type', () => {
const mod = loadModule()
assert.throws(() => mod.normalizeNodeInputs({ id: 'n', inputs: [{ label: 'x' }] }), /n inputs\[0\] must be a type name/)
assert.throws(() => mod.normalizeNodeInputs({ id: 'n', inputs: ['image', null] }), /inputs\[1\]/)
})

test('validateInstallManifest sees object-shaped scene slots as scene inputs', () => {
const mod = loadModule()
const files = { hasEntryFile: () => true, hasGeneratorFile: () => true }
assert.throws(() => mod.validateInstallManifest({
id: 'proc', type: 'process', entry: 'processor.js',
nodes: [{ id: 'run', inputs: [{ type: 'scene' }] }],
}, files, 'repository'), /scene input and output are supported only for model nodes/)
})
51 changes: 50 additions & 1 deletion electron/main/extension-install-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ export interface InstallManifest {
id?: string
input?: unknown
inputs?: unknown
input_labels?: unknown
output?: unknown
hf_repo?: unknown
model_sources?: unknown
Expand All @@ -43,6 +44,54 @@ export interface ExtensionReloadPayload {
errors: Record<string, string>
}

export interface NormalizedNodeInputs {
/** One type name per input slot, or undefined for a single-input node. */
inputs?: string[]
/** Display label per slot: explicit `input_labels`, else the objects' labels. */
inputLabels?: string[]
/** Set only when at least one slot is optional; true means required. */
inputsRequired?: boolean[]
}

/**
* A manifest may declare `inputs` as type names (`["image", "text"]`) or as
* slot objects (`[{ "type": "image", "label": "Front", "required": false }]`).
* Everything downstream compares slots against type names, so objects are
* reduced to their type here and their label/required carried separately.
*/
export function normalizeNodeInputs(node: { id?: unknown; inputs?: unknown; input_labels?: unknown }): NormalizedNodeInputs {
const explicitLabels = Array.isArray(node.input_labels) ? node.input_labels as string[] : undefined
if (!Array.isArray(node.inputs)) return { inputLabels: explicitLabels }

const inputs: string[] = []
const labels: (string | undefined)[] = []
const required: boolean[] = []
node.inputs.forEach((entry: unknown, i: number) => {
if (typeof entry === 'string') {
inputs.push(entry)
labels.push(undefined)
required.push(true)
return
}
const slot = entry as { type?: unknown; label?: unknown; name?: unknown; required?: unknown } | null
if (!slot || typeof slot !== 'object' || typeof slot.type !== 'string') {
throw new Error(`manifest.json: ${String(node.id ?? 'node')} inputs[${i}] must be a type name or an object with a "type"`)
}
inputs.push(slot.type)
labels.push(typeof slot.label === 'string' ? slot.label : typeof slot.name === 'string' ? slot.name : undefined)
required.push(slot.required !== false)
})

const derivedLabels = labels.some((label) => label !== undefined)
? labels.map((label, i) => label ?? inputs[i])
: undefined
return {
inputs,
inputLabels: explicitLabels ?? derivedLabels,
inputsRequired: required.every(Boolean) ? undefined : required,
}
}

export function assertSupportedSceneNodeShape(
kind: 'model' | 'process',
node: { id?: string; input?: unknown; inputs?: unknown; output?: unknown },
Expand Down Expand Up @@ -100,7 +149,7 @@ export function validateInstallManifest(
validateModelNodeIds(manifest.nodes ?? [])
}
for (const node of Array.isArray(manifest.nodes) ? manifest.nodes : []) {
const declaredInputs = Array.isArray(node.inputs) ? node.inputs : [node.input ?? 'image']
const declaredInputs = normalizeNodeInputs(node).inputs ?? [node.input ?? 'image']
const output = node.output ?? 'mesh'
assertSupportedSceneNodeShape(isProcess ? 'process' : 'model', node, declaredInputs, output)
const usesSharedWeights = weightGroups !== undefined || node.weight_groups !== undefined
Expand Down
11 changes: 7 additions & 4 deletions electron/main/ipc-handlers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ import {
validateExistingExtensionReplacement,
validateInstallManifest,
assertSupportedSceneNodeShape,
normalizeNodeInputs,
} from './extension-install-utils'
import {
beginExtensionRegistrationTransaction,
Expand Down Expand Up @@ -1225,7 +1226,7 @@ export function setupIpcHandlers(pythonBridge: PythonBridge, getWindow: WindowGe
id: string
name?: string
input?: string
inputs?: string[]
inputs?: unknown[] // type names, or { type, label?, required? } slot objects
input_labels?: string[]
output?: string
params_schema?: unknown[]
Expand Down Expand Up @@ -1263,7 +1264,8 @@ export function setupIpcHandlers(pythonBridge: PythonBridge, getWindow: WindowGe
validateModelNodeIds(parsed.nodes ?? [])
}
const nodes = (parsed.nodes ?? []).map(n => {
const declaredInputs = Array.isArray(n.inputs) ? n.inputs : [n.input ?? 'image']
const slots = normalizeNodeInputs(n)
const declaredInputs = slots.inputs ?? [n.input ?? 'image']
const output = n.output ?? 'mesh'
assertSupportedSceneNodeShape(parsed.type === 'process' ? 'process' : 'model', n, declaredInputs, output)
const usesManagedWeights = weightGroups !== undefined
Expand Down Expand Up @@ -1295,8 +1297,9 @@ export function setupIpcHandlers(pythonBridge: PythonBridge, getWindow: WindowGe
id: nodeId,
name: n.name ?? n.id,
input: n.input ?? 'image' as const,
inputs: n.inputs,
inputLabels: n.input_labels,
inputs: slots.inputs,
inputLabels: slots.inputLabels,
inputsRequired: slots.inputsRequired,
output: n.output ?? 'mesh' as const,
paramsSchema: n.params_schema ?? parsed.params_schema ?? [],
paramDefaults: { ...(parsed.param_defaults ?? {}), ...(n.param_defaults ?? {}) },
Expand Down
3 changes: 3 additions & 0 deletions src/areas/workflows/mockExtensions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ export interface WorkflowExtension {
input: 'image' | 'text' | 'mesh' | 'audio' | 'scene'
inputs?: ('image' | 'text' | 'mesh' | 'audio' | 'scene')[] // multi-input; overrides input when set
inputLabels?: string[] // display labels per input slot
inputsRequired?: boolean[] // per slot; absent means all required
output: 'image' | 'text' | 'mesh' | 'audio' | 'scene'
params: ParamSchema[]
builtin: boolean
Expand Down Expand Up @@ -52,6 +53,7 @@ export function buildAllWorkflowExtensions(
input: node.input,
inputs: node.inputs,
inputLabels: node.inputLabels,
inputsRequired: node.inputsRequired,
output: node.output,
params: applyParamDefaults(node.paramsSchema as ParamSchema[], node.paramDefaults),
builtin: ext.builtin,
Expand All @@ -74,6 +76,7 @@ export function buildAllWorkflowExtensions(
input: node.input,
inputs: node.inputs,
inputLabels: node.inputLabels,
inputsRequired: node.inputsRequired,
output: node.output,
params: applyParamDefaults(node.paramsSchema as ParamSchema[], node.paramDefaults),
builtin: ext.builtin,
Expand Down
27 changes: 27 additions & 0 deletions src/areas/workflows/preflight.test.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,33 @@ test('multi-input extension requires every declared input type', () => {
assert.ok(issues.some((i) => i.key === 'proc:missing:text'))
})

test('optional input slots are not required, but their type is still accepted (#241)', () => {
const { validateWorkflowPreflight } = loadModule()
const proc = { id: 'proc', type: 'extensionNode', position: { x: 0, y: 0 }, data: { extensionId: 'pack/process-node' } }
// image-to-image: required front image, optional second image
const imageToImage = ext({ inputs: ['image', 'image'], inputsRequired: [true, false] })
assert.deepEqual(
validateWorkflowPreflight(wf([imageNode('img'), proc], [{ id: 'e1', source: 'img', target: 'proc', targetHandle: 'input-0' }]), [imageToImage]),
[],
)

// an optional text prompt next to a required image
const withPrompt = ext({ inputs: ['image', 'text'], inputsRequired: [true, false] })
assert.deepEqual(validateWorkflowPreflight(wf([imageNode('img'), proc], [{ id: 'e1', source: 'img', target: 'proc' }]), [withPrompt]), [])
const both = validateWorkflowPreflight(
wf([imageNode('img'), textNode('txt'), proc], [{ id: 'e1', source: 'img', target: 'proc' }, { id: 'e2', source: 'txt', target: 'proc' }]),
[withPrompt],
)
assert.deepEqual(both, [])
})

test('missing type names the real type instead of defaulting to text', () => {
const { validateWorkflowPreflight } = loadModule()
const proc = { id: 'proc', type: 'extensionNode', position: { x: 0, y: 0 }, data: { extensionId: 'pack/process-node' } }
const issues = validateWorkflowPreflight(wf([proc]), [ext({ input: 'pointcloud' })])
assert.match(issues[0].message, /needs an incoming pointcloud connection/)
})

test('scene input requires a validated Load Scene source and rejects image wiring', () => {
const { validateWorkflowPreflight } = loadModule()
const model = { id: 'model', type: 'extensionNode', position: { x: 0, y: 0 }, data: { extensionId: 'pack/process-node' } }
Expand Down
15 changes: 7 additions & 8 deletions src/areas/workflows/preflight.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,7 @@ function nodeLabel(node: WFNode, allExtensions: WorkflowExtension[]): string {
}

function formatType(type: DataType): string {
if (type === 'scene') return 'scene'
if (type === 'mesh') return 'mesh'
if (type === 'image') return 'image'
if (type === 'audio') return 'audio'
return 'text'
return String(type)
}

function formatRequiredTypes(types: DataType[]): string {
Expand Down Expand Up @@ -142,7 +138,10 @@ export function validateWorkflowPreflight(
}

const incomingEdges = workflow.edges.filter((edge) => edge.target === node.id)
const requiredTypes = [...new Set((ext.inputs ?? [ext.input]) as DataType[])]
const slotTypes = (ext.inputs ?? [ext.input]) as DataType[]
const acceptedTypes = [...new Set(slotTypes)]
// A type is required when at least one slot of that type is not optional.
const requiredTypes = [...new Set(slotTypes.filter((_, i) => ext.inputsRequired?.[i] !== false))]

for (const requiredType of requiredTypes) {
const hasMatchingInput = incomingEdges.some((edge) => outputTypes.get(edge.source) === requiredType)
Expand All @@ -158,11 +157,11 @@ export function validateWorkflowPreflight(
for (const edge of incomingEdges) {
const sourceNode = nodeMap.get(edge.source)
const sourceType = outputTypes.get(edge.source)
if (!sourceNode || !sourceType || requiredTypes.includes(sourceType)) continue
if (!sourceNode || !sourceType || acceptedTypes.includes(sourceType)) continue
pushIssue(issues, {
key: `${node.id}:type:${edge.id}`,
nodeId: node.id,
message: `${ext.name} expects ${formatRequiredTypes(requiredTypes)}, but ${nodeLabel(sourceNode, allExtensions)} outputs ${formatType(sourceType)}.`,
message: `${ext.name} expects ${formatRequiredTypes(acceptedTypes)}, but ${nodeLabel(sourceNode, allExtensions)} outputs ${formatType(sourceType)}.`,
})
}
}
Expand Down
1 change: 1 addition & 0 deletions src/shared/types/electron.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ export interface ExtensionNode {
input: 'image' | 'text' | 'mesh' | 'audio' | 'scene'
inputs?: ('image' | 'text' | 'mesh' | 'audio' | 'scene')[] // multi-input nodes; overrides input when set
inputLabels?: string[] // display labels per input slot (e.g. positive/negative)
inputsRequired?: boolean[] // per input slot; absent means every slot is required
output: 'image' | 'text' | 'mesh' | 'audio' | 'scene'
paramsSchema: ParamSchema[]
paramDefaults?: Record<string, number | string>
Expand Down
Loading