Skip to content

Commit feefcba

Browse files
committed
Allows CustomFunction to be set as an Ending Node
1 parent 1104257 commit feefcba

File tree

2 files changed

+35
-16
lines changed

2 files changed

+35
-16
lines changed

packages/components/nodes/utilities/CustomFunction/CustomFunction.ts

+12-1
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,11 @@ class CustomFunction_Utilities implements INode {
5252
label: 'Output',
5353
name: 'output',
5454
baseClasses: ['string', 'number', 'boolean', 'json', 'array']
55+
},
56+
{
57+
label: 'Ending Node',
58+
name: 'EndingNode',
59+
baseClasses: [this.type]
5560
}
5661
]
5762
}
@@ -123,14 +128,20 @@ class CustomFunction_Utilities implements INode {
123128
const vm = new NodeVM(nodeVMOptions)
124129
try {
125130
const response = await vm.run(`module.exports = async function() {${javascriptFunction}}()`, __dirname)
126-
if (typeof response === 'string') {
131+
const isEndingNode = Object.values(nodeData.outputs ?? {}).includes('EndingNode')
132+
133+
if (typeof response === 'string' && !isEndingNode) {
127134
return handleEscapeCharacters(response, false)
128135
}
129136
return response
130137
} catch (e) {
131138
throw new Error(e)
132139
}
133140
}
141+
142+
async run(nodeData: INodeData, input: string, options: ICommonObject): Promise<string> {
143+
return await this.init(nodeData, input, options)
144+
}
134145
}
135146

136147
module.exports = { nodeClass: CustomFunction_Utilities }

packages/server/src/index.ts

+23-15
Original file line numberDiff line numberDiff line change
@@ -465,8 +465,12 @@ export class App {
465465
const endingNodeData = endingNode.data
466466
if (!endingNodeData) return res.status(500).send(`Ending node ${endingNode.id} data not found`)
467467

468-
if (endingNodeData && endingNodeData.category !== 'Chains' && endingNodeData.category !== 'Agents') {
469-
return res.status(500).send(`Ending node must be either a Chain or Agent`)
468+
const isEndingNode = Object.values(endingNodeData.outputs ?? {}).includes('EndingNode')
469+
470+
if (!isEndingNode) {
471+
if (endingNodeData && endingNodeData.category !== 'Chains' && endingNodeData.category !== 'Agents') {
472+
return res.status(500).send(`Ending node must be either a Chain or Agent`)
473+
}
470474
}
471475

472476
isStreaming = isFlowValidForStream(nodes, endingNodeData)
@@ -1652,20 +1656,24 @@ export class App {
16521656
const endingNodeData = endingNode.data
16531657
if (!endingNodeData) return res.status(500).send(`Ending node ${endingNode.id} data not found`)
16541658

1655-
if (endingNodeData && endingNodeData.category !== 'Chains' && endingNodeData.category !== 'Agents') {
1656-
return res.status(500).send(`Ending node must be either a Chain or Agent`)
1657-
}
1659+
const isEndingNode = Object.values(endingNodeData.outputs ?? {}).includes('EndingNode')
16581660

1659-
if (
1660-
endingNodeData.outputs &&
1661-
Object.keys(endingNodeData.outputs).length &&
1662-
!Object.values(endingNodeData.outputs).includes(endingNodeData.name)
1663-
) {
1664-
return res
1665-
.status(500)
1666-
.send(
1667-
`Output of ${endingNodeData.label} (${endingNodeData.id}) must be ${endingNodeData.label}, can't be an Output Prediction`
1668-
)
1661+
if (!isEndingNode) {
1662+
if (endingNodeData && endingNodeData.category !== 'Chains' && endingNodeData.category !== 'Agents') {
1663+
return res.status(500).send(`Ending node must be either a Chain or Agent`)
1664+
}
1665+
1666+
if (
1667+
endingNodeData.outputs &&
1668+
Object.keys(endingNodeData.outputs).length &&
1669+
!Object.values(endingNodeData.outputs).includes(endingNodeData.name)
1670+
) {
1671+
return res
1672+
.status(500)
1673+
.send(
1674+
`Output of ${endingNodeData.label} (${endingNodeData.id}) must be ${endingNodeData.label}, can't be an Output Prediction`
1675+
)
1676+
}
16691677
}
16701678

16711679
isStreamValid = isFlowValidForStream(nodes, endingNodeData)

0 commit comments

Comments
 (0)