Skip to content

Commit 601a4d6

Browse files
authored
Merge pull request #1591 from mokeyish/patch-2
Allows CustomFunction to be set as an Ending Node.
2 parents 09d2b96 + 50cef64 commit 601a4d6

File tree

2 files changed

+33
-16
lines changed

2 files changed

+33
-16
lines changed

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

+14-1
Original file line numberDiff line numberDiff line change
@@ -52,11 +52,19 @@ 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
}
5863

5964
async init(nodeData: INodeData, input: string, options: ICommonObject): Promise<any> {
65+
const isEndingNode = nodeData?.outputs?.output === 'EndingNode'
66+
if (isEndingNode && !options.isRun) return // prevent running both init and run twice
67+
6068
const javascriptFunction = nodeData.inputs?.javascriptFunction as string
6169
const functionInputVariablesRaw = nodeData.inputs?.functionInputVariables
6270
const appDataSource = options.appDataSource as DataSource
@@ -123,14 +131,19 @@ class CustomFunction_Utilities implements INode {
123131
const vm = new NodeVM(nodeVMOptions)
124132
try {
125133
const response = await vm.run(`module.exports = async function() {${javascriptFunction}}()`, __dirname)
126-
if (typeof response === 'string') {
134+
135+
if (typeof response === 'string' && !isEndingNode) {
127136
return handleEscapeCharacters(response, false)
128137
}
129138
return response
130139
} catch (e) {
131140
throw new Error(e)
132141
}
133142
}
143+
144+
async run(nodeData: INodeData, input: string, options: ICommonObject): Promise<string> {
145+
return await this.init(nodeData, input, { ...options, isRun: true })
146+
}
134147
}
135148

136149
module.exports = { nodeClass: CustomFunction_Utilities }

packages/server/src/index.ts

+19-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 = endingNodeData?.outputs?.output === '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)
@@ -1665,20 +1669,20 @@ export class App {
16651669
const endingNodeData = endingNode.data
16661670
if (!endingNodeData) return res.status(500).send(`Ending node ${endingNode.id} data not found`)
16671671

1668-
if (endingNodeData && endingNodeData.category !== 'Chains' && endingNodeData.category !== 'Agents') {
1669-
return res.status(500).send(`Ending node must be either a Chain or Agent`)
1670-
}
1672+
const isEndingNode = endingNodeData?.outputs?.output === 'EndingNode'
16711673

1672-
if (
1673-
endingNodeData.outputs &&
1674-
Object.keys(endingNodeData.outputs).length &&
1675-
!Object.values(endingNodeData.outputs).includes(endingNodeData.name)
1676-
) {
1677-
return res
1678-
.status(500)
1679-
.send(
1680-
`Output of ${endingNodeData.label} (${endingNodeData.id}) must be ${endingNodeData.label}, can't be an Output Prediction`
1681-
)
1674+
if (!isEndingNode) {
1675+
if (endingNodeData && endingNodeData.category !== 'Chains' && endingNodeData.category !== 'Agents') {
1676+
return res.status(500).send(`Ending node must be either a Chain or Agent`)
1677+
}
1678+
1679+
if (!Object.values(endingNodeData.outputs ?? {}).includes(endingNodeData.name)) {
1680+
return res
1681+
.status(500)
1682+
.send(
1683+
`Output of ${endingNodeData.label} (${endingNodeData.id}) must be ${endingNodeData.label}, can't be an Output Prediction`
1684+
)
1685+
}
16821686
}
16831687

16841688
isStreamValid = isFlowValidForStream(nodes, endingNodeData)

0 commit comments

Comments
 (0)