Skip to content

Commit 2de55aa

Browse files
authored
Merge pull request #167 from SdgSoft/feature/array-node-extract-schema-port
Feature: ArrayNode extract schema port
2 parents b18be39 + 6b37479 commit 2de55aa

1 file changed

Lines changed: 43 additions & 59 deletions

File tree

packages/chaingraph-nodes/src/nodes/basic-values/array.node.ts

Lines changed: 43 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import type {
1313
IPortConfig,
1414
NodeEvent,
1515
NodeExecutionResult,
16+
ObjectPortValue,
1617
PortConnectedEvent,
1718
PortDisconnectedEvent,
1819
} from '@badaitech/chaingraph-types'
@@ -83,7 +84,11 @@ class ArrayNode extends BaseNode {
8384
return
8485
}
8586

87+
// Get the source port and its configuration
8688
const sourcePort = event.sourcePort
89+
if (!sourcePort) {
90+
return
91+
}
8792
const sourcePortConfig = sourcePort.getConfig()
8893

8994
// Only process the itemSchema port and ensure it is an input port without a parent
@@ -211,114 +216,81 @@ class ArrayNode extends BaseNode {
211216
switch (portConfig.type) {
212217
case 'string': {
213218
specificSchema = {
214-
type: portConfig.type,
219+
...portConfig,
215220
defaultValue: portConfig.defaultValue || '',
216-
minLength: portConfig.minLength,
217-
maxLength: portConfig.maxLength,
218-
pattern: portConfig.pattern,
219-
ui: {
220-
isTextArea: portConfig.ui?.isTextArea,
221-
isPassword: portConfig.ui?.isPassword,
222-
textareaDimensions: portConfig.ui?.textareaDimensions,
223-
hideEditor: false,
224-
},
225221
}
226222
break
227223
}
228224
case 'number': {
229225
specificSchema = {
230-
type: portConfig.type,
226+
...portConfig,
231227
defaultValue: portConfig.defaultValue ?? 0,
232-
min: portConfig.min,
233-
max: portConfig.max,
234-
step: portConfig.step,
235-
integer: portConfig.integer,
236-
ui: {
237-
isSlider: portConfig.ui?.isSlider,
238-
leftSliderLabel: portConfig.ui?.leftSliderLabel,
239-
rightSliderLabel: portConfig.ui?.rightSliderLabel,
240-
hideEditor: false,
241-
},
242228
}
243229
break
244230
}
245231
case 'boolean': {
246232
specificSchema = {
247-
type: portConfig.type,
233+
...portConfig,
248234
defaultValue: portConfig.defaultValue ?? false,
249-
ui: {
250-
hideEditor: false,
251-
},
252235
}
253236
break
254237
}
255238
case 'array': {
256239
specificSchema = {
257-
type: portConfig.type,
240+
...portConfig,
258241
itemConfig: this.createPortConfig(portConfig.itemConfig, true),
259242
defaultValue: portConfig.defaultValue || [],
260-
minLength: portConfig.minLength,
261-
maxLength: portConfig.maxLength,
262243
isMutable: true,
263-
ui: {
264-
hideEditor: false,
265-
},
266244
}
267245
break
268246
}
269247
case 'object': {
248+
const objectSchema = this.createObjectSchema(portConfig.schema)
249+
const defaultValue = this.createObjectDefaultValues(objectSchema)
270250
specificSchema = {
271-
type: portConfig.type,
272-
schema: this.createObjectSchema(portConfig.schema),
273-
defaultValue: portConfig.defaultValue || {},
251+
...portConfig,
252+
schema: objectSchema,
253+
defaultValue,
274254
isSchemaMutable: false,
275255
ui: {
276-
hideEditor: false,
277256
keyDeletable: false,
278257
},
279258
}
280259
break
281260
}
282261
case 'enum': {
283262
specificSchema = {
284-
type: portConfig.type,
285-
options: portConfig.options || [],
263+
...portConfig,
286264
defaultValue: portConfig.defaultValue || '',
287-
ui: {
288-
hideEditor: false,
289-
},
290265
}
291266
break
292267
}
293268
case 'stream': {
294269
specificSchema = {
295-
type: portConfig.type,
270+
...portConfig,
296271
itemConfig: this.createPortConfig(portConfig.itemConfig, true),
297-
ui: {
298-
hideEditor: false,
299-
},
300272
}
301273
break
302274
}
303275
case 'any': {
304276
specificSchema = {
305-
type: portConfig.type,
277+
...portConfig,
306278
defaultValue: portConfig.defaultValue,
307-
ui: {
308-
hideEditor: false,
309-
},
310279
}
311280
break
312281
}
313282
}
314283

315-
if (isChildConfig) {
316-
specificSchema = {
317-
...specificSchema,
318-
title: portConfig.title,
319-
description: portConfig.description,
320-
order: portConfig.order,
321-
}
284+
specificSchema = {
285+
...specificSchema,
286+
id: undefined,
287+
title: isChildConfig ? specificSchema.title : undefined,
288+
description: isChildConfig ? specificSchema.description : undefined,
289+
direction: 'output',
290+
ui: {
291+
...specificSchema.ui,
292+
hideEditor: false,
293+
},
322294
}
323295

324296
return specificSchema
@@ -338,13 +310,25 @@ class ArrayNode extends BaseNode {
338310
}
339311

340312
return {
341-
id: schema.id || undefined,
342-
type: schema.type || 'object',
343-
description: schema.description || undefined,
344-
category: schema.category || undefined,
345313
properties,
346314
}
347315
}
316+
317+
/**
318+
* Create default values for object ports based on the schema
319+
*/
320+
private createObjectDefaultValues(schema?: IObjectSchema): ObjectPortValue<any> {
321+
if (!schema || !schema.properties) {
322+
return {}
323+
}
324+
325+
const defaultValue: ObjectPortValue<any> = {}
326+
for (const key in schema.properties) {
327+
defaultValue[key] = schema.properties[key].defaultValue
328+
}
329+
330+
return defaultValue
331+
}
348332
}
349333

350334
export default ArrayNode

0 commit comments

Comments
 (0)