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

Feature: Add support for accessing NodeData via json path #1820

Merged
merged 3 commits into from
Mar 1, 2024
Merged
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
40 changes: 37 additions & 3 deletions packages/server/src/utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -528,11 +528,45 @@ export const getVariableValue = (
variableDict[`{{${variableFullPath}}}`] = handleEscapeCharacters(convertChatHistoryToText(chatHistory), false)
}

// Split by first occurrence of '.' to get just nodeId
const [variableNodeId, _] = variableFullPath.split('.')
// Resolve values with following case.
// 1: <variableNodeId>.data.instance
// 2: <variableNodeId>.data.instance.pathtokey
const variableFullPathParts = variableFullPath.split('.')
const variableNodeId = variableFullPathParts[0]
const executedNode = reactFlowNodes.find((nd) => nd.id === variableNodeId)
if (executedNode) {
const variableValue = get(executedNode.data, 'instance')
let variableValue = get(executedNode.data, 'instance')

// Handle path such as `<variableNodeId>.data.instance.key`
if (variableFullPathParts.length > 3) {
HenryHengZJ marked this conversation as resolved.
Show resolved Hide resolved
let variableObj = null
switch (typeof variableValue) {
case 'string': {
const unEscapedVariableValue = handleEscapeCharacters(variableValue, true)
if (unEscapedVariableValue.startsWith('{') && unEscapedVariableValue.endsWith('}')) {
try {
variableObj = JSON.parse(unEscapedVariableValue)
} catch (e) {
// ignore
}
}
break
}
case 'object': {
variableObj = variableValue
break
}
default:
break
}
if (variableObj) {
variableObj = get(variableObj, variableFullPathParts.slice(3))
variableValue = handleEscapeCharacters(
typeof variableObj === 'object' ? JSON.stringify(variableObj) : variableObj,
false
)
}
}
if (isAcceptVariable) {
variableDict[`{{${variableFullPath}}}`] = variableValue
} else {
Expand Down
Loading