Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make node def output class #231

Merged
merged 2 commits into from
Jul 26, 2024
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
2 changes: 1 addition & 1 deletion src/components/NodePreview.vue
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ const nodeDefStore = useNodeDefStore()

const nodeDef = props.nodeDef
const allInputDefs = nodeDef.input.all
const allOutputDefs = Object.values(nodeDef.output)
const allOutputDefs = nodeDef.output.all
const slotInputDefs = allInputDefs.filter(
(input) => !nodeDefStore.inputIsWidget(input)
)
Expand Down
35 changes: 19 additions & 16 deletions src/stores/nodeDefStore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -132,15 +132,21 @@ export class ComfyInputsSpec {
}

export class ComfyOutputSpec {
type: string
is_list: boolean
display_name: string
name?: string
comboOptions?: any[]
constructor(
public name: string,
public display_name: string,
public type: string,
public is_list: boolean,
public comboOptions?: any[]
) {}
}

export class ComfyOutputsSpec {
[key: string]: ComfyOutputSpec
constructor(public outputByName: Record<string, ComfyOutputSpec>) {}

get all() {
return Object.values(this.outputByName)
}
}

export class ComfyNodeDefImpl {
Expand All @@ -158,31 +164,28 @@ export class ComfyNodeDefImpl {

private static transformOutputSpec(obj: any): ComfyOutputsSpec {
const { output, output_is_list, output_name } = obj
const result: ComfyOutputsSpec = {}
const result = {}

output.forEach((type: string | any[], index: number) => {
const typeString = Array.isArray(type) ? 'COMBO' : type
const display_name = output_name[index]
const name = display_name === typeString ? index.toString() : display_name

const outputSpec = {
const outputSpec = new ComfyOutputSpec(
name,
display_name,
type: typeString,
is_list: output_is_list[index]
} as ComfyOutputSpec

if (Array.isArray(type)) {
outputSpec.comboOptions = type
}
typeString,
output_is_list[index],
Array.isArray(type) ? type : undefined
)

if (name in result) {
throw new Error(`Duplicate output name: ${name}`)
}
result[name] = outputSpec
})

return result
return new ComfyOutputsSpec(result)
}
}

Expand Down
8 changes: 4 additions & 4 deletions tests-ui/tests/nodeDef.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ describe('ComfyNodeDefImpl', () => {
expect(result.python_module).toBe('test_module')
expect(result.description).toBe('A test node')
expect(result.input).toBeInstanceOf(ComfyInputsSpec)
expect(result.output).toEqual({
expect(result.output.outputByName).toEqual({
intOutput: {
name: 'intOutput',
display_name: 'intOutput',
Expand All @@ -196,7 +196,7 @@ describe('ComfyNodeDefImpl', () => {

const result = plainToClass(ComfyNodeDefImpl, plainObject)

expect(result.output).toEqual({
expect(result.output.outputByName).toEqual({
stringOutput: {
name: 'stringOutput',
display_name: 'stringOutput',
Expand Down Expand Up @@ -234,7 +234,7 @@ describe('ComfyNodeDefImpl', () => {

const result = plainToClass(ComfyNodeDefImpl, plainObject)

expect(result.output).toEqual({
expect(result.output.outputByName).toEqual({
'0': {
name: '0',
display_name: 'INT',
Expand Down Expand Up @@ -289,7 +289,7 @@ describe('ComfyNodeDefImpl', () => {

const result = plainToClass(ComfyNodeDefImpl, plainObject)

expect(result.output).toEqual({})
expect(result.output.outputByName).toEqual({})
})

it('should handle complex input specifications', () => {
Expand Down
Loading